Commit 5454ddbc9b6be1b3a8fe1018bda697ac2b33b202

Authored by Christian Hitz
Committed by Scott Wood
1 parent ff8a8a7183

nand: Sanitize ONFI strings.

[backport from linux commit 02f8c6aee8df3cdc935e9bdd4f2d020306035dbe]

This is part of the synchronization with the nand driver to the
Linux 3.0 state.

Signed-off-by: Christian Hitz <christian.hitz@aizo.com>
Cc: Scott Wood <scottwood@freescale.com>
Signed-off-by: Scott Wood <scottwood@freescale.com>

Showing 3 changed files with 65 additions and 1 deletions Side-by-side Diff

drivers/mtd/nand/nand_base.c
... ... @@ -2477,10 +2477,29 @@
2477 2477 }
2478 2478  
2479 2479 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  2480 +/*
  2481 + * sanitize ONFI strings so we can safely print them
  2482 + */
  2483 +static void sanitize_string(char *s, size_t len)
  2484 +{
  2485 + ssize_t i;
  2486 +
  2487 + /* null terminate */
  2488 + s[len - 1] = 0;
  2489 +
  2490 + /* remove non printable chars */
  2491 + for (i = 0; i < len - 1; i++) {
  2492 + if (s[i] < ' ' || s[i] > 127)
  2493 + s[i] = '?';
  2494 + }
  2495 +
  2496 + /* remove trailing spaces */
  2497 + strim(s);
  2498 +}
  2499 +
2480 2500 static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2481 2501 {
2482 2502 int i;
2483   -
2484 2503 while (len--) {
2485 2504 crc ^= *p++ << 8;
2486 2505 for (i = 0; i < 8; i++)
... ... @@ -2541,6 +2560,8 @@
2541 2560 return 0;
2542 2561 }
2543 2562  
  2563 + sanitize_string(p->manufacturer, sizeof(p->manufacturer));
  2564 + sanitize_string(p->model, sizeof(p->model));
2544 2565 if (!mtd->name)
2545 2566 mtd->name = p->model;
2546 2567 mtd->writesize = le32_to_cpu(p->byte_per_page);
include/linux/string.h
... ... @@ -47,6 +47,10 @@
47 47 #ifndef __HAVE_ARCH_STRRCHR
48 48 extern char * strrchr(const char *,int);
49 49 #endif
  50 +extern char * skip_spaces(const char *);
  51 +
  52 +extern char *strim(char *);
  53 +
50 54 #ifndef __HAVE_ARCH_STRSTR
51 55 extern char * strstr(const char *,const char *);
52 56 #endif
... ... @@ -214,6 +214,45 @@
214 214 }
215 215 #endif
216 216  
  217 +
  218 +/**
  219 + * skip_spaces - Removes leading whitespace from @str.
  220 + * @str: The string to be stripped.
  221 + *
  222 + * Returns a pointer to the first non-whitespace character in @str.
  223 + */
  224 +char *skip_spaces(const char *str)
  225 +{
  226 + while (isspace(*str))
  227 + ++str;
  228 + return (char *)str;
  229 +}
  230 +
  231 +/**
  232 + * strim - Removes leading and trailing whitespace from @s.
  233 + * @s: The string to be stripped.
  234 + *
  235 + * Note that the first trailing whitespace is replaced with a %NUL-terminator
  236 + * in the given string @s. Returns a pointer to the first non-whitespace
  237 + * character in @s.
  238 + */
  239 +char *strim(char *s)
  240 +{
  241 + size_t size;
  242 + char *end;
  243 +
  244 + s = skip_spaces(s);
  245 + size = strlen(s);
  246 + if (!size)
  247 + return s;
  248 +
  249 + end = s + size - 1;
  250 + while (end >= s && isspace(*end))
  251 + end--;
  252 + *(end + 1) = '\0';
  253 +
  254 + return s;
  255 +}
217 256 #ifndef __HAVE_ARCH_STRLEN
218 257 /**
219 258 * strlen - Find the length of a string