Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame^] | 1 | #include <common.h> |
| 2 | |
| 3 | #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) |
| 4 | |
| 5 | #include <malloc.h> |
| 6 | #include <linux/stat.h> |
| 7 | #include <linux/time.h> |
| 8 | |
| 9 | #include <jffs2/jffs2.h> |
| 10 | #include <jffs2/jffs2_1pass.h> |
| 11 | #include <nand.h> |
| 12 | |
| 13 | #include "jffs2_nand_private.h" |
| 14 | |
| 15 | #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */ |
| 16 | |
| 17 | /* Debugging switches */ |
| 18 | #undef DEBUG_DIRENTS /* print directory entry list after scan */ |
| 19 | #undef DEBUG_FRAGMENTS /* print fragment list after scan */ |
| 20 | #undef DEBUG /* enable debugging messages */ |
| 21 | |
| 22 | #ifdef DEBUG |
| 23 | # define DEBUGF(fmt,args...) printf(fmt ,##args) |
| 24 | #else |
| 25 | # define DEBUGF(fmt,args...) |
| 26 | #endif |
| 27 | |
| 28 | static int nanddev = -1; /* nand device of current partition */ |
| 29 | static nand_info_t *nand; |
| 30 | |
| 31 | /* Compression names */ |
| 32 | static char *compr_names[] = { |
| 33 | "NONE", |
| 34 | "ZERO", |
| 35 | "RTIME", |
| 36 | "RUBINMIPS", |
| 37 | "COPY", |
| 38 | "DYNRUBIN", |
| 39 | "ZLIB", |
| 40 | #if defined(CONFIG_JFFS2_LZO_LZARI) |
| 41 | "LZO", |
| 42 | "LZARI", |
| 43 | #endif |
| 44 | }; |
| 45 | |
| 46 | /* Spinning wheel */ |
| 47 | static char spinner[] = { '|', '/', '-', '\\' }; |
| 48 | |
| 49 | /* Memory management */ |
| 50 | struct mem_block { |
| 51 | unsigned index; |
| 52 | struct mem_block *next; |
| 53 | char nodes[0]; |
| 54 | }; |
| 55 | |
| 56 | static void |
| 57 | free_nodes(struct b_list *list) |
| 58 | { |
| 59 | while (list->listMemBase != NULL) { |
| 60 | struct mem_block *next = list->listMemBase->next; |
| 61 | free(list->listMemBase); |
| 62 | list->listMemBase = next; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static struct b_node * |
| 67 | add_node(struct b_list *list, int size) |
| 68 | { |
| 69 | u32 index = 0; |
| 70 | struct mem_block *memBase; |
| 71 | struct b_node *b; |
| 72 | |
| 73 | memBase = list->listMemBase; |
| 74 | if (memBase != NULL) |
| 75 | index = memBase->index; |
| 76 | |
| 77 | if (memBase == NULL || index >= NODE_CHUNK) { |
| 78 | /* we need more space before we continue */ |
| 79 | memBase = mmalloc(sizeof(struct mem_block) + NODE_CHUNK * size); |
| 80 | if (memBase == NULL) { |
| 81 | putstr("add_node: malloc failed\n"); |
| 82 | return NULL; |
| 83 | } |
| 84 | memBase->next = list->listMemBase; |
| 85 | index = 0; |
| 86 | } |
| 87 | /* now we have room to add it. */ |
| 88 | b = (struct b_node *)&memBase->nodes[size * index]; |
| 89 | index ++; |
| 90 | |
| 91 | memBase->index = index; |
| 92 | list->listMemBase = memBase; |
| 93 | list->listCount++; |
| 94 | return b; |
| 95 | } |
| 96 | |
| 97 | static struct b_node * |
| 98 | insert_node(struct b_list *list, struct b_node *new) |
| 99 | { |
| 100 | #ifdef CFG_JFFS2_SORT_FRAGMENTS |
| 101 | struct b_node *b, *prev; |
| 102 | |
| 103 | if (list->listTail != NULL && list->listCompare(new, list->listTail)) |
| 104 | prev = list->listTail; |
| 105 | else if (list->listLast != NULL && list->listCompare(new, list->listLast)) |
| 106 | prev = list->listLast; |
| 107 | else |
| 108 | prev = NULL; |
| 109 | |
| 110 | for (b = (prev ? prev->next : list->listHead); |
| 111 | b != NULL && list->listCompare(new, b); |
| 112 | prev = b, b = b->next) { |
| 113 | list->listLoops++; |
| 114 | } |
| 115 | if (b != NULL) |
| 116 | list->listLast = prev; |
| 117 | |
| 118 | if (b != NULL) { |
| 119 | new->next = b; |
| 120 | if (prev != NULL) |
| 121 | prev->next = new; |
| 122 | else |
| 123 | list->listHead = new; |
| 124 | } else |
| 125 | #endif |
| 126 | { |
| 127 | new->next = (struct b_node *) NULL; |
| 128 | if (list->listTail != NULL) { |
| 129 | list->listTail->next = new; |
| 130 | list->listTail = new; |
| 131 | } else { |
| 132 | list->listTail = list->listHead = new; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return new; |
| 137 | } |
| 138 | |
| 139 | static struct b_node * |
| 140 | insert_inode(struct b_list *list, struct jffs2_raw_inode *node, u32 offset) |
| 141 | { |
| 142 | struct b_inode *new; |
| 143 | |
| 144 | if (!(new = (struct b_inode *)add_node(list, sizeof(struct b_inode)))) { |
| 145 | putstr("add_node failed!\r\n"); |
| 146 | return NULL; |
| 147 | } |
| 148 | new->offset = offset; |
| 149 | new->version = node->version; |
| 150 | new->ino = node->ino; |
| 151 | new->isize = node->isize; |
| 152 | new->csize = node->csize; |
| 153 | |
| 154 | return insert_node(list, (struct b_node *)new); |
| 155 | } |
| 156 | |
| 157 | static struct b_node * |
| 158 | insert_dirent(struct b_list *list, struct jffs2_raw_dirent *node, u32 offset) |
| 159 | { |
| 160 | struct b_dirent *new; |
| 161 | |
| 162 | if (!(new = (struct b_dirent *)add_node(list, sizeof(struct b_dirent)))) { |
| 163 | putstr("add_node failed!\r\n"); |
| 164 | return NULL; |
| 165 | } |
| 166 | new->offset = offset; |
| 167 | new->version = node->version; |
| 168 | new->pino = node->pino; |
| 169 | new->ino = node->ino; |
| 170 | new->nhash = full_name_hash(node->name, node->nsize); |
| 171 | new->nsize = node->nsize; |
| 172 | new->type = node->type; |
| 173 | |
| 174 | return insert_node(list, (struct b_node *)new); |
| 175 | } |
| 176 | |
| 177 | #ifdef CFG_JFFS2_SORT_FRAGMENTS |
| 178 | /* Sort data entries with the latest version last, so that if there |
| 179 | * is overlapping data the latest version will be used. |
| 180 | */ |
| 181 | static int compare_inodes(struct b_node *new, struct b_node *old) |
| 182 | { |
| 183 | struct jffs2_raw_inode ojNew; |
| 184 | struct jffs2_raw_inode ojOld; |
| 185 | struct jffs2_raw_inode *jNew = |
| 186 | (struct jffs2_raw_inode *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew); |
| 187 | struct jffs2_raw_inode *jOld = |
| 188 | (struct jffs2_raw_inode *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld); |
| 189 | |
| 190 | return jNew->version > jOld->version; |
| 191 | } |
| 192 | |
| 193 | /* Sort directory entries so all entries in the same directory |
| 194 | * with the same name are grouped together, with the latest version |
| 195 | * last. This makes it easy to eliminate all but the latest version |
| 196 | * by marking the previous version dead by setting the inode to 0. |
| 197 | */ |
| 198 | static int compare_dirents(struct b_node *new, struct b_node *old) |
| 199 | { |
| 200 | struct jffs2_raw_dirent ojNew; |
| 201 | struct jffs2_raw_dirent ojOld; |
| 202 | struct jffs2_raw_dirent *jNew = |
| 203 | (struct jffs2_raw_dirent *)get_fl_mem(new->offset, sizeof(ojNew), &ojNew); |
| 204 | struct jffs2_raw_dirent *jOld = |
| 205 | (struct jffs2_raw_dirent *)get_fl_mem(old->offset, sizeof(ojOld), &ojOld); |
| 206 | int cmp; |
| 207 | |
| 208 | /* ascending sort by pino */ |
| 209 | if (jNew->pino != jOld->pino) |
| 210 | return jNew->pino > jOld->pino; |
| 211 | |
| 212 | /* pino is the same, so use ascending sort by nsize, so |
| 213 | * we don't do strncmp unless we really must. |
| 214 | */ |
| 215 | if (jNew->nsize != jOld->nsize) |
| 216 | return jNew->nsize > jOld->nsize; |
| 217 | |
| 218 | /* length is also the same, so use ascending sort by name |
| 219 | */ |
| 220 | cmp = strncmp(jNew->name, jOld->name, jNew->nsize); |
| 221 | if (cmp != 0) |
| 222 | return cmp > 0; |
| 223 | |
| 224 | /* we have duplicate names in this directory, so use ascending |
| 225 | * sort by version |
| 226 | */ |
| 227 | if (jNew->version > jOld->version) { |
| 228 | /* since jNew is newer, we know jOld is not valid, so |
| 229 | * mark it with inode 0 and it will not be used |
| 230 | */ |
| 231 | jOld->ino = 0; |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | #endif |
| 238 | |
| 239 | static u32 |
| 240 | jffs_init_1pass_list(struct part_info *part) |
| 241 | { |
| 242 | struct b_lists *pL; |
| 243 | |
| 244 | if (part->jffs2_priv != NULL) { |
| 245 | pL = (struct b_lists *)part->jffs2_priv; |
| 246 | free_nodes(&pL->frag); |
| 247 | free_nodes(&pL->dir); |
| 248 | free(pL); |
| 249 | } |
| 250 | if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) { |
| 251 | pL = (struct b_lists *)part->jffs2_priv; |
| 252 | |
| 253 | memset(pL, 0, sizeof(*pL)); |
| 254 | #ifdef CFG_JFFS2_SORT_FRAGMENTS |
| 255 | pL->dir.listCompare = compare_dirents; |
| 256 | pL->frag.listCompare = compare_inodes; |
| 257 | #endif |
| 258 | } |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* find the inode from the slashless name given a parent */ |
| 263 | static long |
| 264 | jffs2_1pass_read_inode(struct b_lists *pL, u32 ino, char *dest, |
| 265 | struct stat *stat) |
| 266 | { |
| 267 | struct b_inode *jNode; |
| 268 | u32 totalSize = 0; |
| 269 | u32 latestVersion = 0; |
| 270 | long ret; |
| 271 | |
| 272 | #ifdef CFG_JFFS2_SORT_FRAGMENTS |
| 273 | /* Find file size before loading any data, so fragments that |
| 274 | * start past the end of file can be ignored. A fragment |
| 275 | * that is partially in the file is loaded, so extra data may |
| 276 | * be loaded up to the next 4K boundary above the file size. |
| 277 | * This shouldn't cause trouble when loading kernel images, so |
| 278 | * we will live with it. |
| 279 | */ |
| 280 | for (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) { |
| 281 | if ((ino == jNode->ino)) { |
| 282 | /* get actual file length from the newest node */ |
| 283 | if (jNode->version >= latestVersion) { |
| 284 | totalSize = jNode->isize; |
| 285 | latestVersion = jNode->version; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | #endif |
| 290 | |
| 291 | for (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) { |
| 292 | if ((ino != jNode->ino)) |
| 293 | continue; |
| 294 | #ifndef CFG_JFFS2_SORT_FRAGMENTS |
| 295 | /* get actual file length from the newest node */ |
| 296 | if (jNode->version >= latestVersion) { |
| 297 | totalSize = jNode->isize; |
| 298 | latestVersion = jNode->version; |
| 299 | } |
| 300 | #endif |
| 301 | if (dest || stat) { |
| 302 | char *src, *dst; |
| 303 | char data[4096 + sizeof(struct jffs2_raw_inode)]; |
| 304 | struct jffs2_raw_inode *inode; |
| 305 | size_t len; |
| 306 | |
| 307 | inode = (struct jffs2_raw_inode *)&data; |
| 308 | len = sizeof(struct jffs2_raw_inode); |
| 309 | if (dest) |
| 310 | len += jNode->csize; |
| 311 | nand_read(nand, jNode->offset, &len, inode); |
| 312 | /* ignore data behind latest known EOF */ |
| 313 | if (inode->offset > totalSize) |
| 314 | continue; |
| 315 | |
| 316 | if (stat) { |
| 317 | stat->st_mtime = inode->mtime; |
| 318 | stat->st_mode = inode->mode; |
| 319 | stat->st_ino = inode->ino; |
| 320 | stat->st_size = totalSize; |
| 321 | } |
| 322 | |
| 323 | if (!dest) |
| 324 | continue; |
| 325 | |
| 326 | src = ((char *) inode) + sizeof(struct jffs2_raw_inode); |
| 327 | dst = (char *) (dest + inode->offset); |
| 328 | |
| 329 | switch (inode->compr) { |
| 330 | case JFFS2_COMPR_NONE: |
| 331 | ret = 0; |
| 332 | memcpy(dst, src, inode->dsize); |
| 333 | break; |
| 334 | case JFFS2_COMPR_ZERO: |
| 335 | ret = 0; |
| 336 | memset(dst, 0, inode->dsize); |
| 337 | break; |
| 338 | case JFFS2_COMPR_RTIME: |
| 339 | ret = 0; |
| 340 | rtime_decompress(src, dst, inode->csize, inode->dsize); |
| 341 | break; |
| 342 | case JFFS2_COMPR_DYNRUBIN: |
| 343 | /* this is slow but it works */ |
| 344 | ret = 0; |
| 345 | dynrubin_decompress(src, dst, inode->csize, inode->dsize); |
| 346 | break; |
| 347 | case JFFS2_COMPR_ZLIB: |
| 348 | ret = zlib_decompress(src, dst, inode->csize, inode->dsize); |
| 349 | break; |
| 350 | #if defined(CONFIG_JFFS2_LZO_LZARI) |
| 351 | case JFFS2_COMPR_LZO: |
| 352 | ret = lzo_decompress(src, dst, inode->csize, inode->dsize); |
| 353 | break; |
| 354 | case JFFS2_COMPR_LZARI: |
| 355 | ret = lzari_decompress(src, dst, inode->csize, inode->dsize); |
| 356 | break; |
| 357 | #endif |
| 358 | default: |
| 359 | /* unknown */ |
| 360 | putLabeledWord("UNKOWN COMPRESSION METHOD = ", inode->compr); |
| 361 | return -1; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return totalSize; |
| 367 | } |
| 368 | |
| 369 | /* find the inode from the slashless name given a parent */ |
| 370 | static u32 |
| 371 | jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino) |
| 372 | { |
| 373 | struct b_dirent *jDir; |
| 374 | int len = strlen(name); /* name is assumed slash free */ |
| 375 | unsigned int nhash = full_name_hash(name, len); |
| 376 | u32 version = 0; |
| 377 | u32 inode = 0; |
| 378 | |
| 379 | /* we need to search all and return the inode with the highest version */ |
| 380 | for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) { |
| 381 | if ((pino == jDir->pino) && (jDir->ino) && /* 0 for unlink */ |
| 382 | (len == jDir->nsize) && (nhash == jDir->nhash)) { |
| 383 | /* TODO: compare name */ |
| 384 | if (jDir->version < version) |
| 385 | continue; |
| 386 | |
| 387 | if (jDir->version == version && inode != 0) { |
| 388 | /* I'm pretty sure this isn't legal */ |
| 389 | putstr(" ** ERROR ** "); |
| 390 | // putnstr(jDir->name, jDir->nsize); |
| 391 | // putLabeledWord(" has dup version =", version); |
| 392 | } |
| 393 | inode = jDir->ino; |
| 394 | version = jDir->version; |
| 395 | } |
| 396 | } |
| 397 | return inode; |
| 398 | } |
| 399 | |
| 400 | char *mkmodestr(unsigned long mode, char *str) |
| 401 | { |
| 402 | static const char *l = "xwr"; |
| 403 | int mask = 1, i; |
| 404 | char c; |
| 405 | |
| 406 | switch (mode & S_IFMT) { |
| 407 | case S_IFDIR: str[0] = 'd'; break; |
| 408 | case S_IFBLK: str[0] = 'b'; break; |
| 409 | case S_IFCHR: str[0] = 'c'; break; |
| 410 | case S_IFIFO: str[0] = 'f'; break; |
| 411 | case S_IFLNK: str[0] = 'l'; break; |
| 412 | case S_IFSOCK: str[0] = 's'; break; |
| 413 | case S_IFREG: str[0] = '-'; break; |
| 414 | default: str[0] = '?'; |
| 415 | } |
| 416 | |
| 417 | for(i = 0; i < 9; i++) { |
| 418 | c = l[i%3]; |
| 419 | str[9-i] = (mode & mask)?c:'-'; |
| 420 | mask = mask<<1; |
| 421 | } |
| 422 | |
| 423 | if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S'; |
| 424 | if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S'; |
| 425 | if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T'; |
| 426 | str[10] = '\0'; |
| 427 | return str; |
| 428 | } |
| 429 | |
| 430 | static inline void dump_stat(struct stat *st, const char *name) |
| 431 | { |
| 432 | char str[20]; |
| 433 | char s[64], *p; |
| 434 | |
| 435 | if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */ |
| 436 | st->st_mtime = 1; |
| 437 | |
| 438 | ctime_r(&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */ |
| 439 | |
| 440 | if ((p = strchr(s,'\n')) != NULL) *p = '\0'; |
| 441 | if ((p = strchr(s,'\r')) != NULL) *p = '\0'; |
| 442 | |
| 443 | /* |
| 444 | printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str), |
| 445 | st->st_size, s, name); |
| 446 | */ |
| 447 | |
| 448 | printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name); |
| 449 | } |
| 450 | |
| 451 | static inline int |
| 452 | dump_inode(struct b_lists *pL, struct b_dirent *d, struct b_inode *i) |
| 453 | { |
| 454 | char fname[JFFS2_MAX_NAME_LEN + 1]; |
| 455 | struct stat st; |
| 456 | size_t len; |
| 457 | |
| 458 | if(!d || !i) return -1; |
| 459 | len = d->nsize; |
| 460 | nand_read(nand, d->offset + sizeof(struct jffs2_raw_dirent), |
| 461 | &len, &fname); |
| 462 | fname[d->nsize] = '\0'; |
| 463 | |
| 464 | memset(&st, 0, sizeof(st)); |
| 465 | |
| 466 | jffs2_1pass_read_inode(pL, i->ino, NULL, &st); |
| 467 | |
| 468 | dump_stat(&st, fname); |
| 469 | /* FIXME |
| 470 | if (d->type == DT_LNK) { |
| 471 | unsigned char *src = (unsigned char *) (&i[1]); |
| 472 | putstr(" -> "); |
| 473 | putnstr(src, (int)i->dsize); |
| 474 | } |
| 475 | */ |
| 476 | putstr("\r\n"); |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | /* list inodes with the given pino */ |
| 482 | static u32 |
| 483 | jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino) |
| 484 | { |
| 485 | struct b_dirent *jDir; |
| 486 | u32 i_version = 0; |
| 487 | |
| 488 | for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) { |
| 489 | if ((pino == jDir->pino) && (jDir->ino)) { /* ino=0 -> unlink */ |
| 490 | struct b_inode *jNode = (struct b_inode *)pL->frag.listHead; |
| 491 | struct b_inode *i = NULL; |
| 492 | |
| 493 | while (jNode) { |
| 494 | if (jNode->ino == jDir->ino && jNode->version >= i_version) { |
| 495 | i_version = jNode->version; |
| 496 | i = jNode; |
| 497 | } |
| 498 | jNode = jNode->next; |
| 499 | } |
| 500 | dump_inode(pL, jDir, i); |
| 501 | } |
| 502 | } |
| 503 | return pino; |
| 504 | } |
| 505 | |
| 506 | static u32 |
| 507 | jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino) |
| 508 | { |
| 509 | int i; |
| 510 | char tmp[256]; |
| 511 | char working_tmp[256]; |
| 512 | char *c; |
| 513 | |
| 514 | /* discard any leading slash */ |
| 515 | i = 0; |
| 516 | while (fname[i] == '/') |
| 517 | i++; |
| 518 | strcpy(tmp, &fname[i]); |
| 519 | |
| 520 | while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */ |
| 521 | { |
| 522 | strncpy(working_tmp, tmp, c - tmp); |
| 523 | working_tmp[c - tmp] = '\0'; |
| 524 | #if 0 |
| 525 | putstr("search_inode: tmp = "); |
| 526 | putstr(tmp); |
| 527 | putstr("\r\n"); |
| 528 | putstr("search_inode: wtmp = "); |
| 529 | putstr(working_tmp); |
| 530 | putstr("\r\n"); |
| 531 | putstr("search_inode: c = "); |
| 532 | putstr(c); |
| 533 | putstr("\r\n"); |
| 534 | #endif |
| 535 | for (i = 0; i < strlen(c) - 1; i++) |
| 536 | tmp[i] = c[i + 1]; |
| 537 | tmp[i] = '\0'; |
| 538 | #if 0 |
| 539 | putstr("search_inode: post tmp = "); |
| 540 | putstr(tmp); |
| 541 | putstr("\r\n"); |
| 542 | #endif |
| 543 | |
| 544 | if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) { |
| 545 | putstr("find_inode failed for name="); |
| 546 | putstr(working_tmp); |
| 547 | putstr("\r\n"); |
| 548 | return 0; |
| 549 | } |
| 550 | } |
| 551 | /* this is for the bare filename, directories have already been mapped */ |
| 552 | if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) { |
| 553 | putstr("find_inode failed for name="); |
| 554 | putstr(tmp); |
| 555 | putstr("\r\n"); |
| 556 | return 0; |
| 557 | } |
| 558 | return pino; |
| 559 | |
| 560 | } |
| 561 | |
| 562 | static u32 |
| 563 | jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino) |
| 564 | { |
| 565 | struct b_dirent *jDir; |
| 566 | struct b_inode *jNode; |
| 567 | u8 jDirFoundType = 0; |
| 568 | u32 jDirFoundIno = 0; |
| 569 | u32 jDirFoundPino = 0; |
| 570 | char tmp[JFFS2_MAX_NAME_LEN + 1]; |
| 571 | u32 version = 0; |
| 572 | u32 pino; |
| 573 | |
| 574 | /* we need to search all and return the inode with the highest version */ |
| 575 | for (jDir = (struct b_dirent *)pL->dir.listHead; jDir; jDir = jDir->next) { |
| 576 | if (ino == jDir->ino) { |
| 577 | if (jDir->version < version) |
| 578 | continue; |
| 579 | |
| 580 | if (jDir->version == version && jDirFoundType) { |
| 581 | /* I'm pretty sure this isn't legal */ |
| 582 | putstr(" ** ERROR ** "); |
| 583 | // putnstr(jDir->name, jDir->nsize); |
| 584 | // putLabeledWord(" has dup version (resolve) = ", |
| 585 | // version); |
| 586 | } |
| 587 | |
| 588 | jDirFoundType = jDir->type; |
| 589 | jDirFoundIno = jDir->ino; |
| 590 | jDirFoundPino = jDir->pino; |
| 591 | version = jDir->version; |
| 592 | } |
| 593 | } |
| 594 | /* now we found the right entry again. (shoulda returned inode*) */ |
| 595 | if (jDirFoundType != DT_LNK) |
| 596 | return jDirFoundIno; |
| 597 | |
| 598 | /* it's a soft link so we follow it again. */ |
| 599 | for (jNode = (struct b_inode *)pL->frag.listHead; jNode; jNode = jNode->next) { |
| 600 | if (jNode->ino == jDirFoundIno) { |
| 601 | size_t len = jNode->csize; |
| 602 | nand_read(nand, jNode->offset + sizeof(struct jffs2_raw_inode), &len, &tmp); |
| 603 | tmp[jNode->csize] = '\0'; |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | /* ok so the name of the new file to find is in tmp */ |
| 608 | /* if it starts with a slash it is root based else shared dirs */ |
| 609 | if (tmp[0] == '/') |
| 610 | pino = 1; |
| 611 | else |
| 612 | pino = jDirFoundPino; |
| 613 | |
| 614 | return jffs2_1pass_search_inode(pL, tmp, pino); |
| 615 | } |
| 616 | |
| 617 | static u32 |
| 618 | jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino) |
| 619 | { |
| 620 | int i; |
| 621 | char tmp[256]; |
| 622 | char working_tmp[256]; |
| 623 | char *c; |
| 624 | |
| 625 | /* discard any leading slash */ |
| 626 | i = 0; |
| 627 | while (fname[i] == '/') |
| 628 | i++; |
| 629 | strcpy(tmp, &fname[i]); |
| 630 | working_tmp[0] = '\0'; |
| 631 | while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */ |
| 632 | { |
| 633 | strncpy(working_tmp, tmp, c - tmp); |
| 634 | working_tmp[c - tmp] = '\0'; |
| 635 | for (i = 0; i < strlen(c) - 1; i++) |
| 636 | tmp[i] = c[i + 1]; |
| 637 | tmp[i] = '\0'; |
| 638 | /* only a failure if we arent looking at top level */ |
| 639 | if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) && |
| 640 | (working_tmp[0])) { |
| 641 | putstr("find_inode failed for name="); |
| 642 | putstr(working_tmp); |
| 643 | putstr("\r\n"); |
| 644 | return 0; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) { |
| 649 | putstr("find_inode failed for name="); |
| 650 | putstr(tmp); |
| 651 | putstr("\r\n"); |
| 652 | return 0; |
| 653 | } |
| 654 | /* this is for the bare filename, directories have already been mapped */ |
| 655 | if (!(pino = jffs2_1pass_list_inodes(pL, pino))) { |
| 656 | putstr("find_inode failed for name="); |
| 657 | putstr(tmp); |
| 658 | putstr("\r\n"); |
| 659 | return 0; |
| 660 | } |
| 661 | return pino; |
| 662 | |
| 663 | } |
| 664 | |
| 665 | unsigned char |
| 666 | jffs2_1pass_rescan_needed(struct part_info *part) |
| 667 | { |
| 668 | struct b_node *b; |
| 669 | struct jffs2_unknown_node onode; |
| 670 | struct jffs2_unknown_node *node; |
| 671 | struct b_lists *pL = (struct b_lists *)part->jffs2_priv; |
| 672 | |
| 673 | if (part->jffs2_priv == 0){ |
| 674 | DEBUGF ("rescan: First time in use\n"); |
| 675 | return 1; |
| 676 | } |
| 677 | /* if we have no list, we need to rescan */ |
| 678 | if (pL->frag.listCount == 0) { |
| 679 | DEBUGF ("rescan: fraglist zero\n"); |
| 680 | return 1; |
| 681 | } |
| 682 | |
| 683 | /* or if we are scanning a new partition */ |
| 684 | if (pL->partOffset != part->offset) { |
| 685 | DEBUGF ("rescan: different partition\n"); |
| 686 | return 1; |
| 687 | } |
| 688 | |
| 689 | #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) |
| 690 | if (nanddev != (int)part->usr_priv - 1) { |
| 691 | DEBUGF ("rescan: nand device changed\n"); |
| 692 | return -1; |
| 693 | } |
| 694 | #endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */ |
| 695 | /* FIXME */ |
| 696 | #if 0 |
| 697 | /* but suppose someone reflashed a partition at the same offset... */ |
| 698 | b = pL->dir.listHead; |
| 699 | while (b) { |
| 700 | node = (struct jffs2_unknown_node *) get_fl_mem(b->offset, |
| 701 | sizeof(onode), &onode); |
| 702 | if (node->nodetype != JFFS2_NODETYPE_DIRENT) { |
| 703 | DEBUGF ("rescan: fs changed beneath me? (%lx)\n", |
| 704 | (unsigned long) b->offset); |
| 705 | return 1; |
| 706 | } |
| 707 | b = b->next; |
| 708 | } |
| 709 | #endif |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | #ifdef DEBUG_FRAGMENTS |
| 714 | static void |
| 715 | dump_fragments(struct b_lists *pL) |
| 716 | { |
| 717 | struct b_node *b; |
| 718 | struct jffs2_raw_inode ojNode; |
| 719 | struct jffs2_raw_inode *jNode; |
| 720 | |
| 721 | putstr("\r\n\r\n******The fragment Entries******\r\n"); |
| 722 | b = pL->frag.listHead; |
| 723 | while (b) { |
| 724 | jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, |
| 725 | sizeof(ojNode), &ojNode); |
| 726 | putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset); |
| 727 | putLabeledWord("\tbuild_list: totlen = ", jNode->totlen); |
| 728 | putLabeledWord("\tbuild_list: inode = ", jNode->ino); |
| 729 | putLabeledWord("\tbuild_list: version = ", jNode->version); |
| 730 | putLabeledWord("\tbuild_list: isize = ", jNode->isize); |
| 731 | putLabeledWord("\tbuild_list: atime = ", jNode->atime); |
| 732 | putLabeledWord("\tbuild_list: offset = ", jNode->offset); |
| 733 | putLabeledWord("\tbuild_list: csize = ", jNode->csize); |
| 734 | putLabeledWord("\tbuild_list: dsize = ", jNode->dsize); |
| 735 | putLabeledWord("\tbuild_list: compr = ", jNode->compr); |
| 736 | putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr); |
| 737 | putLabeledWord("\tbuild_list: flags = ", jNode->flags); |
| 738 | putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ |
| 739 | b = b->next; |
| 740 | } |
| 741 | } |
| 742 | #endif |
| 743 | |
| 744 | #ifdef DEBUG_DIRENTS |
| 745 | static void |
| 746 | dump_dirents(struct b_lists *pL) |
| 747 | { |
| 748 | struct b_node *b; |
| 749 | struct jffs2_raw_dirent *jDir; |
| 750 | |
| 751 | putstr("\r\n\r\n******The directory Entries******\r\n"); |
| 752 | b = pL->dir.listHead; |
| 753 | while (b) { |
| 754 | jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset); |
| 755 | putstr("\r\n"); |
| 756 | putnstr(jDir->name, jDir->nsize); |
| 757 | putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic); |
| 758 | putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype); |
| 759 | putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc); |
| 760 | putLabeledWord("\tbuild_list: pino = ", jDir->pino); |
| 761 | putLabeledWord("\tbuild_list: version = ", jDir->version); |
| 762 | putLabeledWord("\tbuild_list: ino = ", jDir->ino); |
| 763 | putLabeledWord("\tbuild_list: mctime = ", jDir->mctime); |
| 764 | putLabeledWord("\tbuild_list: nsize = ", jDir->nsize); |
| 765 | putLabeledWord("\tbuild_list: type = ", jDir->type); |
| 766 | putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc); |
| 767 | putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc); |
| 768 | putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ |
| 769 | b = b->next; |
| 770 | put_fl_mem(jDir); |
| 771 | } |
| 772 | } |
| 773 | #endif |
| 774 | |
| 775 | static int |
| 776 | jffs2_fill_scan_buf(nand_info_t *nand, unsigned char *buf, |
| 777 | unsigned ofs, unsigned len) |
| 778 | { |
| 779 | int ret; |
| 780 | unsigned olen; |
| 781 | |
| 782 | olen = len; |
| 783 | ret = nand_read(nand, ofs, &olen, buf); |
| 784 | if (ret) { |
| 785 | printf("nand_read(0x%x bytes from 0x%x) returned %d\n", len, ofs, ret); |
| 786 | return ret; |
| 787 | } |
| 788 | if (olen < len) { |
| 789 | printf("Read at 0x%x gave only 0x%x bytes\n", ofs, olen); |
| 790 | return -1; |
| 791 | } |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | #define EMPTY_SCAN_SIZE 1024 |
| 796 | static u32 |
| 797 | jffs2_1pass_build_lists(struct part_info * part) |
| 798 | { |
| 799 | struct b_lists *pL; |
| 800 | struct jffs2_unknown_node *node; |
| 801 | unsigned nr_blocks, sectorsize, ofs, offset; |
| 802 | char *buf; |
| 803 | int i; |
| 804 | u32 counter = 0; |
| 805 | u32 counter4 = 0; |
| 806 | u32 counterF = 0; |
| 807 | u32 counterN = 0; |
| 808 | |
| 809 | #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) |
| 810 | nanddev = (int)part->usr_priv - 1; |
| 811 | nand = &nand_info[nanddev]; |
| 812 | #endif /* defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND) */ |
| 813 | |
| 814 | /* if we are building a list we need to refresh the cache. */ |
| 815 | jffs_init_1pass_list(part); |
| 816 | pL = (struct b_lists *)part->jffs2_priv; |
| 817 | pL->partOffset = part->offset; |
| 818 | puts ("Scanning JFFS2 FS: "); |
| 819 | |
| 820 | sectorsize = nand->erasesize; |
| 821 | nr_blocks = part->size / sectorsize; |
| 822 | buf = malloc(sectorsize); |
| 823 | if (!buf) |
| 824 | return 0; |
| 825 | |
| 826 | for (i = 0; i < nr_blocks; i++) { |
| 827 | printf("\b\b%c ", spinner[counter++ % sizeof(spinner)]); |
| 828 | |
| 829 | offset = part->offset + i * sectorsize; |
| 830 | |
| 831 | if (nand_block_isbad(nand, offset)) |
| 832 | continue; |
| 833 | |
| 834 | if (jffs2_fill_scan_buf(nand, buf, offset, EMPTY_SCAN_SIZE)) |
| 835 | return 0; |
| 836 | |
| 837 | ofs = 0; |
| 838 | /* Scan only 4KiB of 0xFF before declaring it's empty */ |
| 839 | while (ofs < EMPTY_SCAN_SIZE && *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF) |
| 840 | ofs += 4; |
| 841 | if (ofs == EMPTY_SCAN_SIZE) |
| 842 | continue; |
| 843 | |
| 844 | if (jffs2_fill_scan_buf(nand, buf + EMPTY_SCAN_SIZE, offset + EMPTY_SCAN_SIZE, sectorsize - EMPTY_SCAN_SIZE)) |
| 845 | return 0; |
| 846 | offset += ofs; |
| 847 | |
| 848 | while (ofs < sectorsize - sizeof(struct jffs2_unknown_node)) { |
| 849 | node = (struct jffs2_unknown_node *)&buf[ofs]; |
| 850 | if (node->magic != JFFS2_MAGIC_BITMASK || !hdr_crc(node)) { |
| 851 | offset += 4; |
| 852 | ofs += 4; |
| 853 | counter4++; |
| 854 | continue; |
| 855 | } |
| 856 | /* if its a fragment add it */ |
| 857 | if (node->nodetype == JFFS2_NODETYPE_INODE && |
| 858 | inode_crc((struct jffs2_raw_inode *) node)) { |
| 859 | if (insert_inode(&pL->frag, (struct jffs2_raw_inode *) node, |
| 860 | offset) == NULL) { |
| 861 | return 0; |
| 862 | } |
| 863 | } else if (node->nodetype == JFFS2_NODETYPE_DIRENT && |
| 864 | dirent_crc((struct jffs2_raw_dirent *) node) && |
| 865 | dirent_name_crc((struct jffs2_raw_dirent *) node)) { |
| 866 | if (! (counterN%100)) |
| 867 | puts ("\b\b. "); |
| 868 | if (insert_dirent(&pL->dir, (struct jffs2_raw_dirent *) node, |
| 869 | offset) == NULL) { |
| 870 | return 0; |
| 871 | } |
| 872 | counterN++; |
| 873 | } else if (node->nodetype == JFFS2_NODETYPE_CLEANMARKER) { |
| 874 | if (node->totlen != sizeof(struct jffs2_unknown_node)) |
| 875 | printf("OOPS Cleanmarker has bad size " |
| 876 | "%d != %d\n", node->totlen, |
| 877 | sizeof(struct jffs2_unknown_node)); |
| 878 | } else if (node->nodetype == JFFS2_NODETYPE_PADDING) { |
| 879 | if (node->totlen < sizeof(struct jffs2_unknown_node)) |
| 880 | printf("OOPS Padding has bad size " |
| 881 | "%d < %d\n", node->totlen, |
| 882 | sizeof(struct jffs2_unknown_node)); |
| 883 | } else { |
| 884 | printf("Unknown node type: %x len %d " |
| 885 | "offset 0x%x\n", node->nodetype, |
| 886 | node->totlen, offset); |
| 887 | } |
| 888 | offset += ((node->totlen + 3) & ~3); |
| 889 | ofs += ((node->totlen + 3) & ~3); |
| 890 | counterF++; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | putstr("\b\b done.\r\n"); /* close off the dots */ |
| 895 | |
| 896 | #if 0 |
| 897 | putLabeledWord("dir entries = ", pL->dir.listCount); |
| 898 | putLabeledWord("frag entries = ", pL->frag.listCount); |
| 899 | putLabeledWord("+4 increments = ", counter4); |
| 900 | putLabeledWord("+file_offset increments = ", counterF); |
| 901 | #endif |
| 902 | |
| 903 | #ifdef DEBUG_DIRENTS |
| 904 | dump_dirents(pL); |
| 905 | #endif |
| 906 | |
| 907 | #ifdef DEBUG_FRAGMENTS |
| 908 | dump_fragments(pL); |
| 909 | #endif |
| 910 | |
| 911 | /* give visual feedback that we are done scanning the flash */ |
| 912 | led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */ |
| 913 | free(buf); |
| 914 | |
| 915 | return 1; |
| 916 | } |
| 917 | |
| 918 | |
| 919 | static u32 |
| 920 | jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL) |
| 921 | { |
| 922 | struct b_node *b; |
| 923 | struct jffs2_raw_inode ojNode; |
| 924 | struct jffs2_raw_inode *jNode; |
| 925 | int i; |
| 926 | |
| 927 | for (i = 0; i < JFFS2_NUM_COMPR; i++) { |
| 928 | piL->compr_info[i].num_frags = 0; |
| 929 | piL->compr_info[i].compr_sum = 0; |
| 930 | piL->compr_info[i].decompr_sum = 0; |
| 931 | } |
| 932 | /* FIXME |
| 933 | b = pL->frag.listHead; |
| 934 | while (b) { |
| 935 | jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, |
| 936 | sizeof(ojNode), &ojNode); |
| 937 | if (jNode->compr < JFFS2_NUM_COMPR) { |
| 938 | piL->compr_info[jNode->compr].num_frags++; |
| 939 | piL->compr_info[jNode->compr].compr_sum += jNode->csize; |
| 940 | piL->compr_info[jNode->compr].decompr_sum += jNode->dsize; |
| 941 | } |
| 942 | b = b->next; |
| 943 | } |
| 944 | */ |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | |
| 949 | static struct b_lists * |
| 950 | jffs2_get_list(struct part_info * part, const char *who) |
| 951 | { |
| 952 | if (jffs2_1pass_rescan_needed(part)) { |
| 953 | if (!jffs2_1pass_build_lists(part)) { |
| 954 | printf("%s: Failed to scan JFFSv2 file structure\n", who); |
| 955 | return NULL; |
| 956 | } |
| 957 | } |
| 958 | return (struct b_lists *)part->jffs2_priv; |
| 959 | } |
| 960 | |
| 961 | |
| 962 | /* Print directory / file contents */ |
| 963 | u32 |
| 964 | jffs2_1pass_ls(struct part_info * part, const char *fname) |
| 965 | { |
| 966 | struct b_lists *pl; |
| 967 | long ret = 0; |
| 968 | u32 inode; |
| 969 | |
| 970 | if (! (pl = jffs2_get_list(part, "ls"))) |
| 971 | return 0; |
| 972 | |
| 973 | if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) { |
| 974 | putstr("ls: Failed to scan jffs2 file structure\r\n"); |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | #if 0 |
| 979 | putLabeledWord("found file at inode = ", inode); |
| 980 | putLabeledWord("read_inode returns = ", ret); |
| 981 | #endif |
| 982 | |
| 983 | return ret; |
| 984 | } |
| 985 | |
| 986 | |
| 987 | /* Load a file from flash into memory. fname can be a full path */ |
| 988 | u32 |
| 989 | jffs2_1pass_load(char *dest, struct part_info * part, const char *fname) |
| 990 | { |
| 991 | |
| 992 | struct b_lists *pl; |
| 993 | long ret = 0; |
| 994 | u32 inode; |
| 995 | |
| 996 | if (! (pl = jffs2_get_list(part, "load"))) |
| 997 | return 0; |
| 998 | |
| 999 | if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) { |
| 1000 | putstr("load: Failed to find inode\r\n"); |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | /* Resolve symlinks */ |
| 1005 | if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) { |
| 1006 | putstr("load: Failed to resolve inode structure\r\n"); |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | if ((ret = jffs2_1pass_read_inode(pl, inode, dest, NULL)) < 0) { |
| 1011 | putstr("load: Failed to read inode\r\n"); |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname, |
| 1016 | (unsigned long) dest, ret); |
| 1017 | return ret; |
| 1018 | } |
| 1019 | |
| 1020 | /* Return information about the fs on this partition */ |
| 1021 | u32 |
| 1022 | jffs2_1pass_info(struct part_info * part) |
| 1023 | { |
| 1024 | struct b_jffs2_info info; |
| 1025 | struct b_lists *pl; |
| 1026 | int i; |
| 1027 | |
| 1028 | if (! (pl = jffs2_get_list(part, "info"))) |
| 1029 | return 0; |
| 1030 | |
| 1031 | jffs2_1pass_fill_info(pl, &info); |
| 1032 | for (i = 0; i < JFFS2_NUM_COMPR; i++) { |
| 1033 | printf ("Compression: %s\n" |
| 1034 | "\tfrag count: %d\n" |
| 1035 | "\tcompressed sum: %d\n" |
| 1036 | "\tuncompressed sum: %d\n", |
| 1037 | compr_names[i], |
| 1038 | info.compr_info[i].num_frags, |
| 1039 | info.compr_info[i].compr_sum, |
| 1040 | info.compr_info[i].decompr_sum); |
| 1041 | } |
| 1042 | return 1; |
| 1043 | } |
| 1044 | |
| 1045 | #endif /* CFG_CMD_JFFS2 */ |