]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - fs/jffs2/jffs2_1pass.c
Cleanup nand_info[] declaration.
[people/ms/u-boot.git] / fs / jffs2 / jffs2_1pass.c
index 7bfcc0484df5d0a276e59376e94a00c23b6f191c..7e27ee18a2a4fcb6b9b83703f7829eff973f0387 100644 (file)
@@ -52,7 +52,7 @@
  * for a bootloader as small and simple as possible. Instead of worring about
  * unneccesary data copies, node scans, etc, I just optimized for the known
  * common case, a kernel, which looks like:
- *     (1) most pages are 4096 bytes
+ *     (1) most pages are 4096 bytes
  *     (2) version numbers are somewhat sorted in acsending order
  *     (3) multiple compressed blocks making up one page is uncommon
  *
 #include <linux/stat.h>
 #include <linux/time.h>
 
-#if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
+#if defined(CONFIG_CMD_JFFS2)
 
 #include <jffs2/jffs2.h>
 #include <jffs2/jffs2_1pass.h>
 #include "jffs2_private.h"
 
 
-#define        NODE_CHUNK      1024    /* size of memory allocation chunk in b_nodes */
-#define        SPIN_BLKSIZE    18      /* spin after having scanned 1<<BLKSIZE bytes */
+#define        NODE_CHUNK      1024    /* size of memory allocation chunk in b_nodes */
+#define        SPIN_BLKSIZE    18      /* spin after having scanned 1<<BLKSIZE bytes */
 
 /* Debugging switches */
 #undef DEBUG_DIRENTS           /* print directory entry list after scan */
 #undef DEBUG_FRAGMENTS         /* print fragment list after scan */
-#undef DEBUG                           /* enable debugging messages */
+#undef DEBUG                   /* enable debugging messages */
 
 
 #ifdef  DEBUG
 # define DEBUGF(fmt,args...)
 #endif
 
-#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
+/* keeps pointer to currentlu processed partition */
+static struct part_info *current_part;
 
+#if (defined(CONFIG_JFFS2_NAND) && \
+     defined(CONFIG_CMD_NAND) )
+#if defined(CFG_NAND_LEGACY)
+#include <linux/mtd/nand_legacy.h>
+#else
+#include <nand.h>
+#endif
 /*
  * Support for jffs2 on top of NAND-flash
  *
  *
  */
 
-/* this one defined in cmd_nand.c */
+#if defined(CFG_NAND_LEGACY)
+/* this one defined in nand_legacy.c */
 int read_jffs2_nand(size_t start, size_t len,
-                   size_t * retlen, u_char * buf, int nanddev);
+               size_t * retlen, u_char * buf, int nanddev);
+#endif
 
 #define NAND_PAGE_SIZE 512
 #define NAND_PAGE_SHIFT 9
@@ -167,10 +177,10 @@ int read_jffs2_nand(size_t start, size_t len,
 
 static u8* nand_cache = NULL;
 static u32 nand_cache_off = (u32)-1;
-static int nanddev = 0; /* nand device of current partition */
 
 static int read_nand_cached(u32 off, u32 size, u_char *buf)
 {
+       struct mtdids *id = current_part->dev->id;
        u32 bytes_read = 0;
        size_t retlen;
        int cpy_bytes;
@@ -189,13 +199,25 @@ static int read_nand_cached(u32 off, u32 size, u_char *buf)
                                        return -1;
                                }
                        }
+
+#if defined(CFG_NAND_LEGACY)
                        if (read_jffs2_nand(nand_cache_off, NAND_CACHE_SIZE,
-                                           &retlen, nand_cache, nanddev) < 0 ||
-                           retlen != NAND_CACHE_SIZE) {
+                                               &retlen, nand_cache, id->num) < 0 ||
+                                       retlen != NAND_CACHE_SIZE) {
                                printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
-                                      nand_cache_off, NAND_CACHE_SIZE);
+                                               nand_cache_off, NAND_CACHE_SIZE);
                                return -1;
                        }
+#else
+                       retlen = NAND_CACHE_SIZE;
+                       if (nand_read(&nand_info[id->num], nand_cache_off,
+                                               &retlen, nand_cache) != 0 ||
+                                       retlen != NAND_CACHE_SIZE) {
+                               printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
+                                               nand_cache_off, NAND_CACHE_SIZE);
+                               return -1;
+                       }
+#endif
                }
                cpy_bytes = nand_cache_off + NAND_CACHE_SIZE - (off + bytes_read);
                if (cpy_bytes > size - bytes_read)
@@ -208,31 +230,32 @@ static int read_nand_cached(u32 off, u32 size, u_char *buf)
        return bytes_read;
 }
 
-static void *get_fl_mem(u32 off, u32 size, void *ext_buf)
+static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf)
 {
        u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
 
        if (NULL == buf) {
-               printf("get_fl_mem: can't alloc %d bytes\n", size);
+               printf("get_fl_mem_nand: can't alloc %d bytes\n", size);
                return NULL;
        }
        if (read_nand_cached(off, size, buf) < 0) {
-               free(buf);
+               if (!ext_buf)
+                       free(buf);
                return NULL;
        }
 
        return buf;
 }
 
-static void *get_node_mem(u32 off)
+static void *get_node_mem_nand(u32 off)
 {
        struct jffs2_unknown_node node;
        void *ret = NULL;
 
-       if (NULL == get_fl_mem(off, sizeof(node), &node))
+       if (NULL == get_fl_mem_nand(off, sizeof(node), &node))
                return NULL;
 
-       if (!(ret = get_fl_mem(off, node.magic ==
+       if (!(ret = get_fl_mem_nand(off, node.magic ==
                               JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
                               NULL))) {
                printf("off = %#x magic %#x type %#x node.totlen = %d\n",
@@ -241,29 +264,90 @@ static void *get_node_mem(u32 off)
        return ret;
 }
 
-static void put_fl_mem(void *buf)
+static void put_fl_mem_nand(void *buf)
 {
        free(buf);
 }
+#endif
+
+
+#if defined(CONFIG_CMD_FLASH)
+/*
+ * Support for jffs2 on top of NOR-flash
+ *
+ * NOR flash memory is mapped in processor's address space,
+ * just return address.
+ */
+static inline void *get_fl_mem_nor(u32 off)
+{
+       u32 addr = off;
+       struct mtdids *id = current_part->dev->id;
 
-#else /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
+       extern flash_info_t flash_info[];
+       flash_info_t *flash = &flash_info[id->num];
 
+       addr += flash->start[0];
+       return (void*)addr;
+}
+
+static inline void *get_node_mem_nor(u32 off)
+{
+       return (void*)get_fl_mem_nor(off);
+}
+#endif
+
+
+/*
+ * Generic jffs2 raw memory and node read routines.
+ *
+ */
 static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
 {
+       struct mtdids *id = current_part->dev->id;
+
+#if defined(CONFIG_CMD_FLASH)
+       if (id->type == MTD_DEV_TYPE_NOR)
+               return get_fl_mem_nor(off);
+#endif
+
+#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
+       if (id->type == MTD_DEV_TYPE_NAND)
+               return get_fl_mem_nand(off, size, ext_buf);
+#endif
+
+       printf("get_fl_mem: unknown device type, using raw offset!\n");
        return (void*)off;
 }
 
 static inline void *get_node_mem(u32 off)
 {
+       struct mtdids *id = current_part->dev->id;
+
+#if defined(CONFIG_CMD_FLASH)
+       if (id->type == MTD_DEV_TYPE_NOR)
+               return get_node_mem_nor(off);
+#endif
+
+#if defined(CONFIG_JFFS2_NAND) && \
+    defined(CONFIG_CMD_NAND)
+       if (id->type == MTD_DEV_TYPE_NAND)
+               return get_node_mem_nand(off);
+#endif
+
+       printf("get_node_mem: unknown device type, using raw offset!\n");
        return (void*)off;
 }
 
 static inline void put_fl_mem(void *buf)
 {
-}
-
-#endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
+#if defined(CONFIG_JFFS2_NAND) && \
+    defined(CONFIG_CMD_NAND)
+       struct mtdids *id = current_part->dev->id;
 
+       if (id->type == MTD_DEV_TYPE_NAND)
+               return put_fl_mem_nand(buf);
+#endif
+}
 
 /* Compression names */
 static char *compr_names[] = {
@@ -273,7 +357,11 @@ static char *compr_names[] = {
        "RUBINMIPS",
        "COPY",
        "DYNRUBIN",
-       "ZLIB"
+       "ZLIB",
+#if defined(CONFIG_JFFS2_LZO_LZARI)
+       "LZO",
+       "LZARI",
+#endif
 };
 
 /* Spinning wheel */
@@ -430,7 +518,7 @@ static int compare_dirents(struct b_node *new, struct b_node *old)
 
        /* length is also the same, so use ascending sort by name
         */
-       cmp = strncmp(jNew->name, jOld->name, jNew->nsize);
+       cmp = strncmp((char *)jNew->name, (char *)jOld->name, jNew->nsize);
        if (cmp != 0)
                return cmp > 0;
 
@@ -452,8 +540,8 @@ static int compare_dirents(struct b_node *new, struct b_node *old)
 static u32
 jffs2_scan_empty(u32 start_offset, struct part_info *part)
 {
-       char *max = part->offset + part->size - sizeof(struct jffs2_raw_inode);
-       char *offset = part->offset + start_offset;
+       char *max = (char *)(part->offset + part->size - sizeof(struct jffs2_raw_inode));
+       char *offset = (char *)(part->offset + start_offset);
        u32 off;
 
        while (offset < max &&
@@ -463,11 +551,11 @@ jffs2_scan_empty(u32 start_offset, struct part_info *part)
                if (((u32)offset & ((1 << SPIN_BLKSIZE)-1)) == 0) break;
        }
 
-       return offset - part->offset;
+       return (u32)offset - part->offset;
 }
 
-static u32
-jffs_init_1pass_list(struct part_info *part)
+void
+jffs2_free_cache(struct part_info *part)
 {
        struct b_lists *pL;
 
@@ -477,6 +565,15 @@ jffs_init_1pass_list(struct part_info *part)
                free_nodes(&pL->dir);
                free(pL);
        }
+}
+
+static u32
+jffs_init_1pass_list(struct part_info *part)
+{
+       struct b_lists *pL;
+
+       jffs2_free_cache(part);
+
        if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
                pL = (struct b_lists *)part->jffs2_priv;
 
@@ -497,8 +594,8 @@ jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
        struct jffs2_raw_inode *jNode;
        u32 totalSize = 0;
        u32 latestVersion = 0;
-       char *lDest;
-       char *src;
+       uchar *lDest;
+       uchar *src;
        long ret;
        int i;
        u32 counter = 0;
@@ -549,14 +646,14 @@ jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
 #endif
 
                        if(dest) {
-                               src = ((char *) jNode) + sizeof(struct jffs2_raw_inode);
+                               src = ((uchar *) jNode) + sizeof(struct jffs2_raw_inode);
                                /* ignore data behind latest known EOF */
                                if (jNode->offset > totalSize) {
                                        put_fl_mem(jNode);
                                        continue;
                                }
 
-                               lDest = (char *) (dest + jNode->offset);
+                               lDest = (uchar *) (dest + jNode->offset);
 #if 0
                                putLabeledWord("read_inode: src = ", src);
                                putLabeledWord("read_inode: dest = ", lDest);
@@ -582,6 +679,14 @@ jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
                                case JFFS2_COMPR_ZLIB:
                                        ret = zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
                                        break;
+#if defined(CONFIG_JFFS2_LZO_LZARI)
+                               case JFFS2_COMPR_LZO:
+                                       ret = lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
+                                       break;
+                               case JFFS2_COMPR_LZARI:
+                                       ret = lzari_decompress(src, lDest, jNode->csize, jNode->dsize);
+                                       break;
+#endif
                                default:
                                        /* unknown */
                                        putLabeledWord("UNKOWN COMPRESSION METHOD = ", jNode->compr);
@@ -626,14 +731,14 @@ jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
                jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
                if ((pino == jDir->pino) && (len == jDir->nsize) &&
                    (jDir->ino) &&      /* 0 for unlink */
-                   (!strncmp(jDir->name, name, len))) {        /* a match */
+                   (!strncmp((char *)jDir->name, name, len))) {        /* a match */
                        if (jDir->version < version) {
                                put_fl_mem(jDir);
                                continue;
                        }
 
                        if (jDir->version == version && inode != 0) {
-                               /* I'm pretty sure this isn't legal */
+                               /* I'm pretty sure this isn't legal */
                                putstr(" ** ERROR ** ");
                                putnstr(jDir->name, jDir->nsize);
                                putLabeledWord(" has dup version =", version);
@@ -693,7 +798,7 @@ static inline void dump_stat(struct stat *st, const char *name)
        if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
                st->st_mtime = 1;
 
-       ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
+       ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
 
        if ((p = strchr(s,'\n')) != NULL) *p = '\0';
        if ((p = strchr(s,'\r')) != NULL) *p = '\0';
@@ -713,7 +818,7 @@ static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, st
 
        if(!d || !i) return -1;
 
-       strncpy(fname, d->name, d->nsize);
+       strncpy(fname, (char *)d->name, d->nsize);
        fname[d->nsize] = '\0';
 
        memset(&st,0,sizeof(st));
@@ -756,9 +861,15 @@ jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
                        while (b2) {
                                jNode = (struct jffs2_raw_inode *)
                                        get_fl_mem(b2->offset, sizeof(ojNode), &ojNode);
-                               if (jNode->ino == jDir->ino
-                                   && jNode->version >= i_version)
-                                       i = get_fl_mem(b2->offset, sizeof(*i), NULL);
+                               if (jNode->ino == jDir->ino && jNode->version >= i_version) {
+                                       if (i)
+                                               put_fl_mem(i);
+
+                                       if (jDir->type == DT_LNK)
+                                               i = get_node_mem(b2->offset);
+                                       else
+                                               i = get_fl_mem(b2->offset, sizeof(*i), NULL);
+                               }
                                b2 = b2->next;
                        }
 
@@ -845,13 +956,13 @@ jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
        for(b = pL->dir.listHead; b; b = b->next) {
                jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset);
                if (ino == jDir->ino) {
-                       if (jDir->version < version) {
+                       if (jDir->version < version) {
                                put_fl_mem(jDir);
                                continue;
                        }
 
                        if (jDir->version == version && jDirFoundType) {
-                               /* I'm pretty sure this isn't legal */
+                               /* I'm pretty sure this isn't legal */
                                putstr(" ** ERROR ** ");
                                putnstr(jDir->name, jDir->nsize);
                                putLabeledWord(" has dup version (resolve) = ",
@@ -882,7 +993,7 @@ jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
                        putnstr(src, jNode->dsize);
                        putstr("\r\n");
 #endif
-                       strncpy(tmp, src, jNode->dsize);
+                       strncpy(tmp, (char *)src, jNode->dsize);
                        tmp[jNode->dsize] = '\0';
                        put_fl_mem(jNode);
                        break;
@@ -960,25 +1071,13 @@ jffs2_1pass_rescan_needed(struct part_info *part)
                DEBUGF ("rescan: First time in use\n");
                return 1;
        }
+
        /* if we have no list, we need to rescan */
        if (pL->frag.listCount == 0) {
                DEBUGF ("rescan: fraglist zero\n");
                return 1;
        }
 
-       /* or if we are scanning a new partition */
-       if (pL->partOffset != part->offset) {
-               DEBUGF ("rescan: different partition\n");
-               return 1;
-       }
-
-#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
-       if (nanddev != (int)part->usr_priv - 1) {
-               DEBUGF ("rescan: nand device changed\n");
-               return -1;
-       }
-#endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
-
        /* but suppose someone reflashed a partition at the same offset... */
        b = pL->dir.listHead;
        while (b) {
@@ -1049,7 +1148,7 @@ dump_dirents(struct b_lists *pL)
                putLabeledWord("\tbuild_list: type = ", jDir->type);
                putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
                putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
-               putLabeledWord("\tbuild_list: offset = ", b->offset);   /* FIXME: ? [RS] */
+               putLabeledWord("\tbuild_list: offset = ", b->offset);   /* FIXME: ? [RS] */
                b = b->next;
                put_fl_mem(jDir);
        }
@@ -1068,10 +1167,6 @@ jffs2_1pass_build_lists(struct part_info * part)
        u32 counterF = 0;
        u32 counterN = 0;
 
-#if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
-       nanddev = (int)part->usr_priv - 1;
-#endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */
-
        /* turn off the lcd.  Refreshing the lcd adds 50% overhead to the */
        /* jffs2 list building enterprise nope.  in newer versions the overhead is */
        /* only about 5 %.  not enough to inconvenience people for. */
@@ -1080,13 +1175,12 @@ jffs2_1pass_build_lists(struct part_info * part)
        /* if we are building a list we need to refresh the cache. */
        jffs_init_1pass_list(part);
        pL = (struct b_lists *)part->jffs2_priv;
-       pL->partOffset = part->offset;
        offset = 0;
        puts ("Scanning JFFS2 FS:   ");
 
        /* start at the beginning of the partition */
        while (offset < max) {
-               if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) {
+               if ((oldoffset >> SPIN_BLKSIZE) != (offset >> SPIN_BLKSIZE)) {
                        printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]);
                        oldoffset = offset;
                }
@@ -1095,7 +1189,8 @@ jffs2_1pass_build_lists(struct part_info * part)
                if (node->magic == JFFS2_MAGIC_BITMASK && hdr_crc(node)) {
                        /* if its a fragment add it */
                        if (node->nodetype == JFFS2_NODETYPE_INODE &&
-                                   inode_crc((struct jffs2_raw_inode *) node)) {
+                                   inode_crc((struct jffs2_raw_inode *) node) &&
+                                   data_crc((struct jffs2_raw_inode *) node)) {
                                if (insert_node(&pL->frag, (u32) part->offset +
                                                offset) == NULL) {
                                        put_fl_mem(node);
@@ -1198,6 +1293,9 @@ jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
 static struct b_lists *
 jffs2_get_list(struct part_info * part, const char *who)
 {
+       /* copy requested part_info struct pointer to global location */
+       current_part = part;
+
        if (jffs2_1pass_rescan_needed(part)) {
                if (!jffs2_1pass_build_lists(part)) {
                        printf("%s: Failed to scan JFFSv2 file structure\n", who);
@@ -1213,7 +1311,7 @@ u32
 jffs2_1pass_ls(struct part_info * part, const char *fname)
 {
        struct b_lists *pl;
-       long ret = 0;
+       long ret = 1;
        u32 inode;
 
        if (! (pl = jffs2_get_list(part, "ls")))
@@ -1240,7 +1338,7 @@ jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
 {
 
        struct b_lists *pl;
-       long ret = 0;
+       long ret = 1;
        u32 inode;
 
        if (! (pl  = jffs2_get_list(part, "load")))
@@ -1292,4 +1390,4 @@ jffs2_1pass_info(struct part_info * part)
        return 1;
 }
 
-#endif /* CFG_CMD_JFFS2 */
+#endif