blob: b39943671c98251075776031df3c6d0ad66d8dc4 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
2-------------------------------------------------------------------------
3 * Filename: jffs2.c
4 * Version: $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
5 * Copyright: Copyright (C) 2001, Russ Dill
6 * Author: Russ Dill <Russ.Dill@asu.edu>
7 * Description: Module to load kernel from jffs2
8 *-----------------------------------------------------------------------*/
9/*
10 * some portions of this code are taken from jffs2, and as such, the
11 * following copyright notice is included.
12 *
13 * JFFS2 -- Journalling Flash File System, Version 2.
14 *
15 * Copyright (C) 2001 Red Hat, Inc.
16 *
17 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
18 *
19 * The original JFFS, from which the design for JFFS2 was derived,
20 * was designed and implemented by Axis Communications AB.
21 *
22 * The contents of this file are subject to the Red Hat eCos Public
23 * License Version 1.1 (the "Licence"); you may not use this file
24 * except in compliance with the Licence. You may obtain a copy of
25 * the Licence at http://www.redhat.com/
26 *
27 * Software distributed under the Licence is distributed on an "AS IS"
28 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
29 * See the Licence for the specific language governing rights and
30 * limitations under the Licence.
31 *
32 * The Original Code is JFFS2 - Journalling Flash File System, version 2
33 *
34 * Alternatively, the contents of this file may be used under the
35 * terms of the GNU General Public License version 2 (the "GPL"), in
36 * which case the provisions of the GPL are applicable instead of the
37 * above. If you wish to allow the use of your version of this file
38 * only under the terms of the GPL and not to allow others to use your
39 * version of this file under the RHEPL, indicate your decision by
40 * deleting the provisions above and replace them with the notice and
41 * other provisions required by the GPL. If you do not delete the
42 * provisions above, a recipient may use your version of this file
43 * under either the RHEPL or the GPL.
44 *
45 * $Id: jffs2_1pass.c,v 1.7 2002/01/25 01:56:47 nyet Exp $
46 *
47 */
48
49/* Ok, so anyone who knows the jffs2 code will probably want to get a papar
50 * bag to throw up into before reading this code. I looked through the jffs2
51 * code, the caching scheme is very elegant. I tried to keep the version
52 * for a bootloader as small and simple as possible. Instead of worring about
53 * unneccesary data copies, node scans, etc, I just optimized for the known
54 * common case, a kernel, which looks like:
Wolfgang Denk53677ef2008-05-20 16:00:29 +020055 * (1) most pages are 4096 bytes
wdenkfe8c2802002-11-03 00:38:21 +000056 * (2) version numbers are somewhat sorted in acsending order
57 * (3) multiple compressed blocks making up one page is uncommon
58 *
59 * So I create a linked list of decending version numbers (insertions at the
60 * head), and then for each page, walk down the list, until a matching page
61 * with 4096 bytes is found, and then decompress the watching pages in
62 * reverse order.
63 *
64 */
65
66/*
67 * Adapted by Nye Liu <nyet@zumanetworks.com> and
68 * Rex Feany <rfeany@zumanetworks.com>
69 * on Jan/2002 for U-Boot.
70 *
71 * Clipped out all the non-1pass functions, cleaned up warnings,
72 * wrappers, etc. No major changes to the code.
73 * Please, he really means it when he said have a paper bag
74 * handy. We needed it ;).
75 *
76 */
77
wdenk06d01db2003-03-14 20:47:52 +000078/*
79 * Bugfixing by Kai-Uwe Bloem <kai-uwe.bloem@auerswald.de>, (C) Mar/2003
80 *
81 * - overhaul of the memory management. Removed much of the "paper-bagging"
82 * in that part of the code, fixed several bugs, now frees memory when
83 * partition is changed.
84 * It's still ugly :-(
85 * - fixed a bug in jffs2_1pass_read_inode where the file length calculation
86 * was incorrect. Removed a bit of the paper-bagging as well.
87 * - removed double crc calculation for fragment headers in jffs2_private.h
88 * for speedup.
89 * - scan_empty rewritten in a more "standard" manner (non-paperbag, that is).
90 * - spinning wheel now spins depending on how much memory has been scanned
91 * - lots of small changes all over the place to "improve" readability.
92 * - implemented fragment sorting to ensure that the newest data is copied
93 * if there are multiple copies of fragments for a certain file offset.
94 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020095 * The fragment sorting feature must be enabled by CONFIG_SYS_JFFS2_SORT_FRAGMENTS.
wdenk06d01db2003-03-14 20:47:52 +000096 * Sorting is done while adding fragments to the lists, which is more or less a
97 * bubble sort. This takes a lot of time, and is most probably not an issue if
98 * the boot filesystem is always mounted readonly.
99 *
100 * You should define it if the boot filesystem is mounted writable, and updates
101 * to the boot files are done by copying files to that filesystem.
102 *
103 *
104 * There's a big issue left: endianess is completely ignored in this code. Duh!
105 *
106 *
107 * You still should have paper bags at hand :-(. The code lacks more or less
108 * any comment, and is still arcane and difficult to read in places. As this
wdenkdd875c72004-01-03 21:24:46 +0000109 * might be incompatible with any new code from the jffs2 maintainers anyway,
110 * it should probably be dumped and replaced by something like jffs2reader!
wdenk06d01db2003-03-14 20:47:52 +0000111 */
112
113
wdenkfe8c2802002-11-03 00:38:21 +0000114#include <common.h>
115#include <config.h>
Simon Glassb79fdc72020-05-10 11:39:54 -0600116#include <flash.h>
wdenkfe8c2802002-11-03 00:38:21 +0000117#include <malloc.h>
Tom Rini18e86722013-12-05 14:48:38 -0500118#include <div64.h>
Tom Rini1c8fdf82016-03-27 14:48:36 -0400119#include <linux/compiler.h>
wdenkfe8c2802002-11-03 00:38:21 +0000120#include <linux/stat.h>
121#include <linux/time.h>
Simon Glass3db71102019-11-14 12:57:16 -0700122#include <u-boot/crc.h>
Stuart Wood86d32732008-06-02 16:40:08 -0400123#include <watchdog.h>
wdenkfe8c2802002-11-03 00:38:21 +0000124#include <jffs2/jffs2.h>
125#include <jffs2/jffs2_1pass.h>
Mike Frysinger7b15e2b2012-04-09 13:39:55 +0000126#include <linux/compat.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +0900127#include <linux/errno.h>
wdenkfe8c2802002-11-03 00:38:21 +0000128
129#include "jffs2_private.h"
130
wdenkfe8c2802002-11-03 00:38:21 +0000131
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200132#define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
133#define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */
wdenkfe8c2802002-11-03 00:38:21 +0000134
wdenk06d01db2003-03-14 20:47:52 +0000135/* Debugging switches */
136#undef DEBUG_DIRENTS /* print directory entry list after scan */
137#undef DEBUG_FRAGMENTS /* print fragment list after scan */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200138#undef DEBUG /* enable debugging messages */
wdenk06d01db2003-03-14 20:47:52 +0000139
140
wdenkfe8c2802002-11-03 00:38:21 +0000141#ifdef DEBUG
142# define DEBUGF(fmt,args...) printf(fmt ,##args)
143#else
144# define DEBUGF(fmt,args...)
145#endif
146
Ilya Yanok9b707622008-11-13 19:49:35 +0300147#include "summary.h"
148
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200149/* keeps pointer to currentlu processed partition */
150static struct part_info *current_part;
wdenk998eaae2004-04-18 19:43:36 +0000151
Wolfgang Denke4dbe1b2007-07-05 17:56:27 +0200152#if (defined(CONFIG_JFFS2_NAND) && \
Jon Loeligerdd60d122007-07-09 17:56:50 -0500153 defined(CONFIG_CMD_NAND) )
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100154#include <nand.h>
wdenk998eaae2004-04-18 19:43:36 +0000155/*
156 * Support for jffs2 on top of NAND-flash
157 *
158 * NAND memory isn't mapped in processor's address space,
159 * so data should be fetched from flash before
160 * being processed. This is exactly what functions declared
161 * here do.
162 *
163 */
164
wdenk998eaae2004-04-18 19:43:36 +0000165#define NAND_PAGE_SIZE 512
166#define NAND_PAGE_SHIFT 9
167#define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1))
168
169#ifndef NAND_CACHE_PAGES
170#define NAND_CACHE_PAGES 16
171#endif
172#define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE)
173
174static u8* nand_cache = NULL;
175static u32 nand_cache_off = (u32)-1;
wdenk998eaae2004-04-18 19:43:36 +0000176
177static int read_nand_cached(u32 off, u32 size, u_char *buf)
178{
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200179 struct mtdids *id = current_part->dev->id;
Grygorii Strashkoeb6a5c32017-06-26 19:12:57 -0500180 struct mtd_info *mtd;
wdenk998eaae2004-04-18 19:43:36 +0000181 u32 bytes_read = 0;
Marian Balakowicz6db39702006-04-08 19:08:06 +0200182 size_t retlen;
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300183 size_t toread;
wdenk998eaae2004-04-18 19:43:36 +0000184 int cpy_bytes;
185
Grygorii Strashkoeb6a5c32017-06-26 19:12:57 -0500186 mtd = get_nand_dev_by_index(id->num);
187 if (!mtd)
188 return -1;
189
wdenk998eaae2004-04-18 19:43:36 +0000190 while (bytes_read < size) {
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300191 retlen = NAND_CACHE_SIZE;
192 if( nand_cache_off + retlen > mtd->size )
193 retlen = mtd->size - nand_cache_off;
194
wdenk998eaae2004-04-18 19:43:36 +0000195 if ((off + bytes_read < nand_cache_off) ||
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300196 (off + bytes_read >= nand_cache_off + retlen)) {
wdenk998eaae2004-04-18 19:43:36 +0000197 nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK;
198 if (!nand_cache) {
199 /* This memory never gets freed but 'cause
200 it's a bootloader, nobody cares */
wdenk507bbe32004-04-18 21:13:41 +0000201 nand_cache = malloc(NAND_CACHE_SIZE);
202 if (!nand_cache) {
wdenk998eaae2004-04-18 19:43:36 +0000203 printf("read_nand_cached: can't alloc cache size %d bytes\n",
204 NAND_CACHE_SIZE);
205 return -1;
206 }
207 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100208
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300209 toread = NAND_CACHE_SIZE;
210 if( nand_cache_off + toread > mtd->size )
211 toread = mtd->size - nand_cache_off;
212
213 retlen = toread;
Grygorii Strashkoeb6a5c32017-06-26 19:12:57 -0500214 if (nand_read(mtd, nand_cache_off,
Engling, Uwe41ba7f52017-10-10 14:20:55 +0000215 &retlen, nand_cache) < 0 ||
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300216 retlen != toread) {
Marian Balakowicz6db39702006-04-08 19:08:06 +0200217 printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300218 nand_cache_off, toread);
Marian Balakowicz6db39702006-04-08 19:08:06 +0200219 return -1;
220 }
wdenk998eaae2004-04-18 19:43:36 +0000221 }
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300222 cpy_bytes = nand_cache_off + retlen - (off + bytes_read);
wdenk998eaae2004-04-18 19:43:36 +0000223 if (cpy_bytes > size - bytes_read)
224 cpy_bytes = size - bytes_read;
225 memcpy(buf + bytes_read,
226 nand_cache + off + bytes_read - nand_cache_off,
227 cpy_bytes);
228 bytes_read += cpy_bytes;
229 }
230 return bytes_read;
231}
232
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200233static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf)
wdenk998eaae2004-04-18 19:43:36 +0000234{
235 u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
236
237 if (NULL == buf) {
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200238 printf("get_fl_mem_nand: can't alloc %d bytes\n", size);
wdenk998eaae2004-04-18 19:43:36 +0000239 return NULL;
240 }
241 if (read_nand_cached(off, size, buf) < 0) {
wdenk32877d62004-05-05 19:44:41 +0000242 if (!ext_buf)
243 free(buf);
wdenk998eaae2004-04-18 19:43:36 +0000244 return NULL;
245 }
246
247 return buf;
248}
249
Ilya Yanok70741002008-11-13 19:49:34 +0300250static void *get_node_mem_nand(u32 off, void *ext_buf)
wdenk998eaae2004-04-18 19:43:36 +0000251{
252 struct jffs2_unknown_node node;
253 void *ret = NULL;
254
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200255 if (NULL == get_fl_mem_nand(off, sizeof(node), &node))
wdenk998eaae2004-04-18 19:43:36 +0000256 return NULL;
257
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200258 if (!(ret = get_fl_mem_nand(off, node.magic ==
wdenk998eaae2004-04-18 19:43:36 +0000259 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
Ilya Yanok70741002008-11-13 19:49:34 +0300260 ext_buf))) {
wdenk998eaae2004-04-18 19:43:36 +0000261 printf("off = %#x magic %#x type %#x node.totlen = %d\n",
262 off, node.magic, node.nodetype, node.totlen);
263 }
264 return ret;
265}
266
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200267static void put_fl_mem_nand(void *buf)
wdenk998eaae2004-04-18 19:43:36 +0000268{
269 free(buf);
270}
Jon Loeligerdd60d122007-07-09 17:56:50 -0500271#endif
wdenk998eaae2004-04-18 19:43:36 +0000272
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900273#if defined(CONFIG_CMD_ONENAND)
274
275#include <linux/mtd/mtd.h>
276#include <linux/mtd/onenand.h>
277#include <onenand_uboot.h>
278
279#define ONENAND_PAGE_SIZE 2048
280#define ONENAND_PAGE_SHIFT 11
281#define ONENAND_PAGE_MASK (~(ONENAND_PAGE_SIZE-1))
282
283#ifndef ONENAND_CACHE_PAGES
284#define ONENAND_CACHE_PAGES 4
285#endif
286#define ONENAND_CACHE_SIZE (ONENAND_CACHE_PAGES*ONENAND_PAGE_SIZE)
287
288static u8* onenand_cache;
289static u32 onenand_cache_off = (u32)-1;
290
291static int read_onenand_cached(u32 off, u32 size, u_char *buf)
292{
293 u32 bytes_read = 0;
294 size_t retlen;
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300295 size_t toread;
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900296 int cpy_bytes;
297
298 while (bytes_read < size) {
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300299 retlen = ONENAND_CACHE_SIZE;
300 if( onenand_cache_off + retlen > onenand_mtd.size )
301 retlen = onenand_mtd.size - onenand_cache_off;
302
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900303 if ((off + bytes_read < onenand_cache_off) ||
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300304 (off + bytes_read >= onenand_cache_off + retlen)) {
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900305 onenand_cache_off = (off + bytes_read) & ONENAND_PAGE_MASK;
306 if (!onenand_cache) {
307 /* This memory never gets freed but 'cause
308 it's a bootloader, nobody cares */
309 onenand_cache = malloc(ONENAND_CACHE_SIZE);
310 if (!onenand_cache) {
311 printf("read_onenand_cached: can't alloc cache size %d bytes\n",
312 ONENAND_CACHE_SIZE);
313 return -1;
314 }
315 }
316
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300317 toread = ONENAND_CACHE_SIZE;
318 if( onenand_cache_off + toread > onenand_mtd.size )
319 toread = onenand_mtd.size - onenand_cache_off;
320 retlen = toread;
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900321 if (onenand_read(&onenand_mtd, onenand_cache_off, retlen,
Engling, Uwe41ba7f52017-10-10 14:20:55 +0000322 &retlen, onenand_cache) < 0 ||
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300323 retlen != toread) {
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900324 printf("read_onenand_cached: error reading nand off %#x size %d bytes\n",
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300325 onenand_cache_off, toread);
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900326 return -1;
327 }
328 }
Wagner Popov dos Santos610a2cc2021-02-22 23:30:58 -0300329 cpy_bytes = onenand_cache_off + retlen - (off + bytes_read);
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900330 if (cpy_bytes > size - bytes_read)
331 cpy_bytes = size - bytes_read;
332 memcpy(buf + bytes_read,
333 onenand_cache + off + bytes_read - onenand_cache_off,
334 cpy_bytes);
335 bytes_read += cpy_bytes;
336 }
337 return bytes_read;
338}
339
340static void *get_fl_mem_onenand(u32 off, u32 size, void *ext_buf)
341{
342 u_char *buf = ext_buf ? (u_char *)ext_buf : (u_char *)malloc(size);
343
344 if (NULL == buf) {
345 printf("get_fl_mem_onenand: can't alloc %d bytes\n", size);
346 return NULL;
347 }
348 if (read_onenand_cached(off, size, buf) < 0) {
349 if (!ext_buf)
350 free(buf);
351 return NULL;
352 }
353
354 return buf;
355}
356
Ilya Yanok70741002008-11-13 19:49:34 +0300357static void *get_node_mem_onenand(u32 off, void *ext_buf)
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900358{
359 struct jffs2_unknown_node node;
360 void *ret = NULL;
361
362 if (NULL == get_fl_mem_onenand(off, sizeof(node), &node))
363 return NULL;
364
365 ret = get_fl_mem_onenand(off, node.magic ==
366 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
Ilya Yanok70741002008-11-13 19:49:34 +0300367 ext_buf);
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900368 if (!ret) {
369 printf("off = %#x magic %#x type %#x node.totlen = %d\n",
370 off, node.magic, node.nodetype, node.totlen);
371 }
372 return ret;
373}
374
375
376static void put_fl_mem_onenand(void *buf)
377{
378 free(buf);
379}
380#endif
381
wdenk998eaae2004-04-18 19:43:36 +0000382
Jon Loeligerdd60d122007-07-09 17:56:50 -0500383#if defined(CONFIG_CMD_FLASH)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200384/*
385 * Support for jffs2 on top of NOR-flash
386 *
387 * NOR flash memory is mapped in processor's address space,
388 * just return address.
389 */
Ilya Yanok70741002008-11-13 19:49:34 +0300390static inline void *get_fl_mem_nor(u32 off, u32 size, void *ext_buf)
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200391{
392 u32 addr = off;
393 struct mtdids *id = current_part->dev->id;
394
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200395 extern flash_info_t flash_info[];
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200396 flash_info_t *flash = &flash_info[id->num];
397
398 addr += flash->start[0];
Ilya Yanok70741002008-11-13 19:49:34 +0300399 if (ext_buf) {
400 memcpy(ext_buf, (void *)addr, size);
401 return ext_buf;
402 }
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200403 return (void*)addr;
404}
405
Ilya Yanok70741002008-11-13 19:49:34 +0300406static inline void *get_node_mem_nor(u32 off, void *ext_buf)
Ilya Yanok8a36d312008-11-13 19:49:33 +0300407{
Ilya Yanok70741002008-11-13 19:49:34 +0300408 struct jffs2_unknown_node *pNode;
Ilya Yanok8a36d312008-11-13 19:49:33 +0300409
Ilya Yanok70741002008-11-13 19:49:34 +0300410 /* pNode will point directly to flash - don't provide external buffer
411 and don't care about size */
412 pNode = get_fl_mem_nor(off, 0, NULL);
413 return (void *)get_fl_mem_nor(off, pNode->magic == JFFS2_MAGIC_BITMASK ?
414 pNode->totlen : sizeof(*pNode), ext_buf);
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200415}
Jon Loeligerdd60d122007-07-09 17:56:50 -0500416#endif
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200417
418
419/*
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200420 * Generic jffs2 raw memory and node read routines.
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200421 *
422 */
wdenk998eaae2004-04-18 19:43:36 +0000423static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
424{
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200425 struct mtdids *id = current_part->dev->id;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200426
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100427 switch(id->type) {
Jon Loeligerdd60d122007-07-09 17:56:50 -0500428#if defined(CONFIG_CMD_FLASH)
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100429 case MTD_DEV_TYPE_NOR:
Ilya Yanok70741002008-11-13 19:49:34 +0300430 return get_fl_mem_nor(off, size, ext_buf);
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100431 break;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200432#endif
Jon Loeligerdd60d122007-07-09 17:56:50 -0500433#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100434 case MTD_DEV_TYPE_NAND:
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200435 return get_fl_mem_nand(off, size, ext_buf);
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100436 break;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200437#endif
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900438#if defined(CONFIG_CMD_ONENAND)
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100439 case MTD_DEV_TYPE_ONENAND:
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900440 return get_fl_mem_onenand(off, size, ext_buf);
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100441 break;
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900442#endif
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100443 default:
444 printf("get_fl_mem: unknown device type, " \
445 "using raw offset!\n");
446 }
wdenk998eaae2004-04-18 19:43:36 +0000447 return (void*)off;
448}
449
Ilya Yanok70741002008-11-13 19:49:34 +0300450static inline void *get_node_mem(u32 off, void *ext_buf)
wdenk998eaae2004-04-18 19:43:36 +0000451{
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200452 struct mtdids *id = current_part->dev->id;
Wolfgang Denk8f79e4c2005-08-10 15:14:32 +0200453
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100454 switch(id->type) {
Jon Loeligerdd60d122007-07-09 17:56:50 -0500455#if defined(CONFIG_CMD_FLASH)
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100456 case MTD_DEV_TYPE_NOR:
Ilya Yanok70741002008-11-13 19:49:34 +0300457 return get_node_mem_nor(off, ext_buf);
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100458 break;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200459#endif
Wolfgang Denke4dbe1b2007-07-05 17:56:27 +0200460#if defined(CONFIG_JFFS2_NAND) && \
Jon Loeligerdd60d122007-07-09 17:56:50 -0500461 defined(CONFIG_CMD_NAND)
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100462 case MTD_DEV_TYPE_NAND:
Ilya Yanok70741002008-11-13 19:49:34 +0300463 return get_node_mem_nand(off, ext_buf);
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100464 break;
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200465#endif
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900466#if defined(CONFIG_CMD_ONENAND)
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100467 case MTD_DEV_TYPE_ONENAND:
Ilya Yanok70741002008-11-13 19:49:34 +0300468 return get_node_mem_onenand(off, ext_buf);
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100469 break;
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900470#endif
Heiko Schocher2d2018f2010-03-24 13:22:50 +0100471 default:
472 printf("get_fl_mem: unknown device type, " \
473 "using raw offset!\n");
474 }
wdenk998eaae2004-04-18 19:43:36 +0000475 return (void*)off;
476}
477
Ilya Yanok70741002008-11-13 19:49:34 +0300478static inline void put_fl_mem(void *buf, void *ext_buf)
wdenk998eaae2004-04-18 19:43:36 +0000479{
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200480 struct mtdids *id = current_part->dev->id;
481
Ilya Yanok70741002008-11-13 19:49:34 +0300482 /* If buf is the same as ext_buf, it was provided by the caller -
483 we shouldn't free it then. */
484 if (buf == ext_buf)
485 return;
Scott Wood2f77c7f2008-10-31 13:51:12 -0500486 switch (id->type) {
487#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
488 case MTD_DEV_TYPE_NAND:
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200489 return put_fl_mem_nand(buf);
490#endif
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900491#if defined(CONFIG_CMD_ONENAND)
Scott Wood2f77c7f2008-10-31 13:51:12 -0500492 case MTD_DEV_TYPE_ONENAND:
Kyungmin Park1a7f8cc2008-08-27 14:45:20 +0900493 return put_fl_mem_onenand(buf);
494#endif
Scott Wood2f77c7f2008-10-31 13:51:12 -0500495 }
wdenk998eaae2004-04-18 19:43:36 +0000496}
497
wdenk06d01db2003-03-14 20:47:52 +0000498/* Compression names */
499static char *compr_names[] = {
500 "NONE",
501 "ZERO",
502 "RTIME",
503 "RUBINMIPS",
504 "COPY",
505 "DYNRUBIN",
wdenk07cc0992005-05-05 00:04:14 +0000506 "ZLIB",
Wolfgang Denkf0983372010-01-15 11:10:33 +0100507#if defined(CONFIG_JFFS2_LZO)
wdenk07cc0992005-05-05 00:04:14 +0000508 "LZO",
wdenk07cc0992005-05-05 00:04:14 +0000509#endif
wdenk06d01db2003-03-14 20:47:52 +0000510};
511
wdenk06d01db2003-03-14 20:47:52 +0000512/* Memory management */
513struct mem_block {
514 u32 index;
515 struct mem_block *next;
516 struct b_node nodes[NODE_CHUNK];
517};
518
519
520static void
521free_nodes(struct b_list *list)
522{
523 while (list->listMemBase != NULL) {
524 struct mem_block *next = list->listMemBase->next;
525 free( list->listMemBase );
526 list->listMemBase = next;
527 }
528}
wdenkfe8c2802002-11-03 00:38:21 +0000529
530static struct b_node *
wdenk06d01db2003-03-14 20:47:52 +0000531add_node(struct b_list *list)
wdenkfe8c2802002-11-03 00:38:21 +0000532{
wdenk06d01db2003-03-14 20:47:52 +0000533 u32 index = 0;
534 struct mem_block *memBase;
wdenkfe8c2802002-11-03 00:38:21 +0000535 struct b_node *b;
536
wdenk06d01db2003-03-14 20:47:52 +0000537 memBase = list->listMemBase;
538 if (memBase != NULL)
539 index = memBase->index;
wdenkfe8c2802002-11-03 00:38:21 +0000540#if 0
541 putLabeledWord("add_node: index = ", index);
wdenk06d01db2003-03-14 20:47:52 +0000542 putLabeledWord("add_node: memBase = ", list->listMemBase);
wdenkfe8c2802002-11-03 00:38:21 +0000543#endif
544
wdenk06d01db2003-03-14 20:47:52 +0000545 if (memBase == NULL || index >= NODE_CHUNK) {
546 /* we need more space before we continue */
547 memBase = mmalloc(sizeof(struct mem_block));
548 if (memBase == NULL) {
wdenkfe8c2802002-11-03 00:38:21 +0000549 putstr("add_node: malloc failed\n");
550 return NULL;
551 }
wdenk06d01db2003-03-14 20:47:52 +0000552 memBase->next = list->listMemBase;
553 index = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000554#if 0
555 putLabeledWord("add_node: alloced a new membase at ", *memBase);
556#endif
557
558 }
559 /* now we have room to add it. */
wdenk06d01db2003-03-14 20:47:52 +0000560 b = &memBase->nodes[index];
561 index ++;
wdenkfe8c2802002-11-03 00:38:21 +0000562
wdenk06d01db2003-03-14 20:47:52 +0000563 memBase->index = index;
564 list->listMemBase = memBase;
565 list->listCount++;
wdenkfe8c2802002-11-03 00:38:21 +0000566 return b;
wdenkfe8c2802002-11-03 00:38:21 +0000567}
568
wdenk06d01db2003-03-14 20:47:52 +0000569static struct b_node *
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200570insert_node(struct b_list *list)
wdenk06d01db2003-03-14 20:47:52 +0000571{
572 struct b_node *new;
wdenk06d01db2003-03-14 20:47:52 +0000573
574 if (!(new = add_node(list))) {
575 putstr("add_node failed!\r\n");
576 return NULL;
577 }
Mark Tomlinson10d3ac32015-07-01 16:38:29 +1200578 new->next = NULL;
wdenk06d01db2003-03-14 20:47:52 +0000579
Mark Tomlinson10d3ac32015-07-01 16:38:29 +1200580 if (list->listTail != NULL)
581 list->listTail->next = new;
wdenk06d01db2003-03-14 20:47:52 +0000582 else
Mark Tomlinson10d3ac32015-07-01 16:38:29 +1200583 list->listHead = new;
584 list->listTail = new;
wdenk06d01db2003-03-14 20:47:52 +0000585
586 return new;
587}
588
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200589#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
wdenk7205e402003-09-10 22:30:53 +0000590/* Sort data entries with the latest version last, so that if there
591 * is overlapping data the latest version will be used.
592 */
wdenk06d01db2003-03-14 20:47:52 +0000593static int compare_inodes(struct b_node *new, struct b_node *old)
594{
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200595 return new->version > old->version;
wdenk06d01db2003-03-14 20:47:52 +0000596}
597
wdenk7205e402003-09-10 22:30:53 +0000598/* Sort directory entries so all entries in the same directory
599 * with the same name are grouped together, with the latest version
600 * last. This makes it easy to eliminate all but the latest version
601 * by marking the previous version dead by setting the inode to 0.
602 */
wdenk06d01db2003-03-14 20:47:52 +0000603static int compare_dirents(struct b_node *new, struct b_node *old)
604{
Mark Tomlinson225cf4c2015-07-01 16:38:23 +1200605 /*
606 * Using NULL as the buffer for NOR flash prevents the entire node
607 * being read. This makes most comparisons much quicker as only one
608 * or two entries from the node will be used most of the time.
609 */
610 struct jffs2_raw_dirent *jNew = get_node_mem(new->offset, NULL);
611 struct jffs2_raw_dirent *jOld = get_node_mem(old->offset, NULL);
wdenk7205e402003-09-10 22:30:53 +0000612 int cmp;
Mark Tomlinson225cf4c2015-07-01 16:38:23 +1200613 int ret;
wdenk06d01db2003-03-14 20:47:52 +0000614
Mark Tomlinson225cf4c2015-07-01 16:38:23 +1200615 if (jNew->pino != jOld->pino) {
616 /* ascending sort by pino */
617 ret = jNew->pino > jOld->pino;
618 } else if (jNew->nsize != jOld->nsize) {
619 /*
620 * pino is the same, so use ascending sort by nsize,
621 * so we don't do strncmp unless we really must.
wdenk7205e402003-09-10 22:30:53 +0000622 */
Mark Tomlinson225cf4c2015-07-01 16:38:23 +1200623 ret = jNew->nsize > jOld->nsize;
624 } else {
625 /*
626 * length is also the same, so use ascending sort by name
627 */
628 cmp = strncmp((char *)jNew->name, (char *)jOld->name,
629 jNew->nsize);
630 if (cmp != 0) {
631 ret = cmp > 0;
632 } else {
633 /*
634 * we have duplicate names in this directory,
635 * so use ascending sort by version
636 */
637 ret = jNew->version > jOld->version;
638 }
wdenk7205e402003-09-10 22:30:53 +0000639 }
Mark Tomlinson225cf4c2015-07-01 16:38:23 +1200640 put_fl_mem(jNew, NULL);
641 put_fl_mem(jOld, NULL);
wdenk42d1f032003-10-15 23:53:47 +0000642
Mark Tomlinson225cf4c2015-07-01 16:38:23 +1200643 return ret;
wdenk06d01db2003-03-14 20:47:52 +0000644}
645#endif
wdenkfe8c2802002-11-03 00:38:21 +0000646
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200647void
648jffs2_free_cache(struct part_info *part)
wdenkfe8c2802002-11-03 00:38:21 +0000649{
wdenk06d01db2003-03-14 20:47:52 +0000650 struct b_lists *pL;
wdenkfe8c2802002-11-03 00:38:21 +0000651
wdenk06d01db2003-03-14 20:47:52 +0000652 if (part->jffs2_priv != NULL) {
653 pL = (struct b_lists *)part->jffs2_priv;
654 free_nodes(&pL->frag);
655 free_nodes(&pL->dir);
Ilya Yanok70741002008-11-13 19:49:34 +0300656 free(pL->readbuf);
wdenk06d01db2003-03-14 20:47:52 +0000657 free(pL);
658 }
Wolfgang Denk700a0c62005-08-08 01:03:24 +0200659}
660
661static u32
662jffs_init_1pass_list(struct part_info *part)
663{
664 struct b_lists *pL;
665
666 jffs2_free_cache(part);
667
wdenk06d01db2003-03-14 20:47:52 +0000668 if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
669 pL = (struct b_lists *)part->jffs2_priv;
670
671 memset(pL, 0, sizeof(*pL));
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200672#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
wdenk06d01db2003-03-14 20:47:52 +0000673 pL->dir.listCompare = compare_dirents;
674 pL->frag.listCompare = compare_inodes;
675#endif
wdenkfe8c2802002-11-03 00:38:21 +0000676 }
677 return 0;
678}
679
680/* find the inode from the slashless name given a parent */
681static long
682jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
683{
684 struct b_node *b;
685 struct jffs2_raw_inode *jNode;
wdenk06d01db2003-03-14 20:47:52 +0000686 u32 totalSize = 0;
wdenk7205e402003-09-10 22:30:53 +0000687 u32 latestVersion = 0;
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200688 uchar *lDest;
689 uchar *src;
wdenkfe8c2802002-11-03 00:38:21 +0000690 int i;
691 u32 counter = 0;
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200692
wdenk7205e402003-09-10 22:30:53 +0000693 /* Find file size before loading any data, so fragments that
694 * start past the end of file can be ignored. A fragment
695 * that is partially in the file is loaded, so extra data may
696 * be loaded up to the next 4K boundary above the file size.
697 * This shouldn't cause trouble when loading kernel images, so
698 * we will live with it.
699 */
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200700 int latestOffset = -1;
wdenk7205e402003-09-10 22:30:53 +0000701 for (b = pL->frag.listHead; b != NULL; b = b->next) {
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200702 if (inode == b->ino) {
wdenk7205e402003-09-10 22:30:53 +0000703 /* get actual file length from the newest node */
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200704 if (b->version >= latestVersion) {
705 latestVersion = b->version;
706 latestOffset = b->offset;
wdenk7205e402003-09-10 22:30:53 +0000707 }
708 }
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200709 }
710
711 if (latestOffset >= 0) {
712 jNode = (struct jffs2_raw_inode *)get_fl_mem(latestOffset,
713 sizeof(struct jffs2_raw_inode), pL->readbuf);
714 totalSize = jNode->isize;
Ilya Yanok70741002008-11-13 19:49:34 +0300715 put_fl_mem(jNode, pL->readbuf);
wdenk7205e402003-09-10 22:30:53 +0000716 }
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200717
Mark Tomlinson3799b3f2015-07-01 16:38:22 +1200718 /*
719 * If no destination is provided, we are done.
720 * Just return the total size.
721 */
722 if (!dest)
723 return totalSize;
wdenkfe8c2802002-11-03 00:38:21 +0000724
wdenk06d01db2003-03-14 20:47:52 +0000725 for (b = pL->frag.listHead; b != NULL; b = b->next) {
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200726 if (inode == b->ino) {
727 /*
728 * Copy just the node and not the data at this point,
729 * since we don't yet know if we need this data.
730 */
731 jNode = (struct jffs2_raw_inode *)get_fl_mem(b->offset,
732 sizeof(struct jffs2_raw_inode),
733 pL->readbuf);
wdenk06d01db2003-03-14 20:47:52 +0000734#if 0
wdenkfe8c2802002-11-03 00:38:21 +0000735 putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
736 putLabeledWord("read_inode: inode = ", jNode->ino);
737 putLabeledWord("read_inode: version = ", jNode->version);
738 putLabeledWord("read_inode: isize = ", jNode->isize);
739 putLabeledWord("read_inode: offset = ", jNode->offset);
740 putLabeledWord("read_inode: csize = ", jNode->csize);
741 putLabeledWord("read_inode: dsize = ", jNode->dsize);
742 putLabeledWord("read_inode: compr = ", jNode->compr);
743 putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
744 putLabeledWord("read_inode: flags = ", jNode->flags);
wdenkfe8c2802002-11-03 00:38:21 +0000745#endif
wdenk7205e402003-09-10 22:30:53 +0000746
wdenkfe8c2802002-11-03 00:38:21 +0000747 if(dest) {
Mark Tomlinson2d6d93a2015-07-01 16:38:25 +1200748 /*
749 * Now that the inode has been checked,
750 * read the entire inode, including data.
751 */
752 put_fl_mem(jNode, pL->readbuf);
753 jNode = (struct jffs2_raw_inode *)
754 get_node_mem(b->offset, pL->readbuf);
755 src = ((uchar *)jNode) +
756 sizeof(struct jffs2_raw_inode);
wdenk06d01db2003-03-14 20:47:52 +0000757 /* ignore data behind latest known EOF */
wdenk998eaae2004-04-18 19:43:36 +0000758 if (jNode->offset > totalSize) {
Ilya Yanok70741002008-11-13 19:49:34 +0300759 put_fl_mem(jNode, pL->readbuf);
wdenk06d01db2003-03-14 20:47:52 +0000760 continue;
wdenk998eaae2004-04-18 19:43:36 +0000761 }
Ilya Yanok142a80f2008-11-13 19:49:36 +0300762 if (b->datacrc == CRC_UNKNOWN)
763 b->datacrc = data_crc(jNode) ?
764 CRC_OK : CRC_BAD;
765 if (b->datacrc == CRC_BAD) {
Ilya Yanok70741002008-11-13 19:49:34 +0300766 put_fl_mem(jNode, pL->readbuf);
Ilya Yanok8a36d312008-11-13 19:49:33 +0300767 continue;
768 }
wdenk06d01db2003-03-14 20:47:52 +0000769
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200770 lDest = (uchar *) (dest + jNode->offset);
wdenkfe8c2802002-11-03 00:38:21 +0000771#if 0
wdenk06d01db2003-03-14 20:47:52 +0000772 putLabeledWord("read_inode: src = ", src);
wdenkfe8c2802002-11-03 00:38:21 +0000773 putLabeledWord("read_inode: dest = ", lDest);
wdenkfe8c2802002-11-03 00:38:21 +0000774#endif
775 switch (jNode->compr) {
776 case JFFS2_COMPR_NONE:
Wolfgang Denk16b9afd2011-10-05 22:58:11 +0200777 ldr_memcpy(lDest, src, jNode->dsize);
wdenkfe8c2802002-11-03 00:38:21 +0000778 break;
779 case JFFS2_COMPR_ZERO:
wdenkfe8c2802002-11-03 00:38:21 +0000780 for (i = 0; i < jNode->dsize; i++)
781 *(lDest++) = 0;
782 break;
783 case JFFS2_COMPR_RTIME:
wdenkfe8c2802002-11-03 00:38:21 +0000784 rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
785 break;
786 case JFFS2_COMPR_DYNRUBIN:
787 /* this is slow but it works */
wdenkfe8c2802002-11-03 00:38:21 +0000788 dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
789 break;
790 case JFFS2_COMPR_ZLIB:
Wolfgang Denk16b9afd2011-10-05 22:58:11 +0200791 zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
wdenkfe8c2802002-11-03 00:38:21 +0000792 break;
Wolfgang Denkf0983372010-01-15 11:10:33 +0100793#if defined(CONFIG_JFFS2_LZO)
wdenk07cc0992005-05-05 00:04:14 +0000794 case JFFS2_COMPR_LZO:
Wolfgang Denk16b9afd2011-10-05 22:58:11 +0200795 lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
wdenk07cc0992005-05-05 00:04:14 +0000796 break;
797#endif
wdenkfe8c2802002-11-03 00:38:21 +0000798 default:
799 /* unknown */
Loïc Minier6052cba2011-02-03 22:04:26 +0100800 putLabeledWord("UNKNOWN COMPRESSION METHOD = ", jNode->compr);
Ilya Yanok70741002008-11-13 19:49:34 +0300801 put_fl_mem(jNode, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +0000802 return -1;
803 break;
804 }
805 }
806
wdenkfe8c2802002-11-03 00:38:21 +0000807#if 0
wdenkfe8c2802002-11-03 00:38:21 +0000808 putLabeledWord("read_inode: totalSize = ", totalSize);
wdenkfe8c2802002-11-03 00:38:21 +0000809#endif
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200810 put_fl_mem(jNode, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +0000811 }
wdenkfe8c2802002-11-03 00:38:21 +0000812 counter++;
813 }
wdenkfe8c2802002-11-03 00:38:21 +0000814
815#if 0
wdenk06d01db2003-03-14 20:47:52 +0000816 putLabeledWord("read_inode: returning = ", totalSize);
wdenkfe8c2802002-11-03 00:38:21 +0000817#endif
wdenk06d01db2003-03-14 20:47:52 +0000818 return totalSize;
wdenkfe8c2802002-11-03 00:38:21 +0000819}
820
821/* find the inode from the slashless name given a parent */
822static u32
823jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
824{
825 struct b_node *b;
826 struct jffs2_raw_dirent *jDir;
827 int len;
828 u32 counter;
829 u32 version = 0;
830 u32 inode = 0;
831
832 /* name is assumed slash free */
833 len = strlen(name);
834
835 counter = 0;
836 /* we need to search all and return the inode with the highest version */
wdenk06d01db2003-03-14 20:47:52 +0000837 for(b = pL->dir.listHead; b; b = b->next, counter++) {
Ilya Yanok70741002008-11-13 19:49:34 +0300838 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
839 pL->readbuf);
wdenk06d01db2003-03-14 20:47:52 +0000840 if ((pino == jDir->pino) && (len == jDir->nsize) &&
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200841 (!strncmp((char *)jDir->name, name, len))) { /* a match */
wdenk998eaae2004-04-18 19:43:36 +0000842 if (jDir->version < version) {
Ilya Yanok70741002008-11-13 19:49:34 +0300843 put_fl_mem(jDir, pL->readbuf);
wdenk7205e402003-09-10 22:30:53 +0000844 continue;
wdenk998eaae2004-04-18 19:43:36 +0000845 }
wdenkfe8c2802002-11-03 00:38:21 +0000846
wdenk7205e402003-09-10 22:30:53 +0000847 if (jDir->version == version && inode != 0) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200848 /* I'm pretty sure this isn't legal */
wdenkfe8c2802002-11-03 00:38:21 +0000849 putstr(" ** ERROR ** ");
850 putnstr(jDir->name, jDir->nsize);
851 putLabeledWord(" has dup version =", version);
852 }
853 inode = jDir->ino;
854 version = jDir->version;
855 }
856#if 0
857 putstr("\r\nfind_inode:p&l ->");
858 putnstr(jDir->name, jDir->nsize);
859 putstr("\r\n");
860 putLabeledWord("pino = ", jDir->pino);
861 putLabeledWord("nsize = ", jDir->nsize);
862 putLabeledWord("b = ", (u32) b);
863 putLabeledWord("counter = ", counter);
864#endif
Ilya Yanok70741002008-11-13 19:49:34 +0300865 put_fl_mem(jDir, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +0000866 }
867 return inode;
868}
869
wdenk180d3f72004-01-04 16:28:35 +0000870char *mkmodestr(unsigned long mode, char *str)
wdenkfe8c2802002-11-03 00:38:21 +0000871{
wdenk06d01db2003-03-14 20:47:52 +0000872 static const char *l = "xwr";
873 int mask = 1, i;
874 char c;
wdenkfe8c2802002-11-03 00:38:21 +0000875
wdenk06d01db2003-03-14 20:47:52 +0000876 switch (mode & S_IFMT) {
877 case S_IFDIR: str[0] = 'd'; break;
878 case S_IFBLK: str[0] = 'b'; break;
879 case S_IFCHR: str[0] = 'c'; break;
880 case S_IFIFO: str[0] = 'f'; break;
881 case S_IFLNK: str[0] = 'l'; break;
882 case S_IFSOCK: str[0] = 's'; break;
883 case S_IFREG: str[0] = '-'; break;
884 default: str[0] = '?';
885 }
wdenkfe8c2802002-11-03 00:38:21 +0000886
wdenk06d01db2003-03-14 20:47:52 +0000887 for(i = 0; i < 9; i++) {
888 c = l[i%3];
889 str[9-i] = (mode & mask)?c:'-';
890 mask = mask<<1;
891 }
wdenkfe8c2802002-11-03 00:38:21 +0000892
wdenk06d01db2003-03-14 20:47:52 +0000893 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
894 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
895 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
896 str[10] = '\0';
897 return str;
wdenkfe8c2802002-11-03 00:38:21 +0000898}
899
900static inline void dump_stat(struct stat *st, const char *name)
901{
wdenk06d01db2003-03-14 20:47:52 +0000902 char str[20];
903 char s[64], *p;
wdenkfe8c2802002-11-03 00:38:21 +0000904
wdenk06d01db2003-03-14 20:47:52 +0000905 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
906 st->st_mtime = 1;
wdenkfe8c2802002-11-03 00:38:21 +0000907
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200908 ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
wdenkfe8c2802002-11-03 00:38:21 +0000909
wdenk06d01db2003-03-14 20:47:52 +0000910 if ((p = strchr(s,'\n')) != NULL) *p = '\0';
911 if ((p = strchr(s,'\r')) != NULL) *p = '\0';
wdenkfe8c2802002-11-03 00:38:21 +0000912
913/*
wdenk06d01db2003-03-14 20:47:52 +0000914 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
915 st->st_size, s, name);
wdenkfe8c2802002-11-03 00:38:21 +0000916*/
917
wdenk06d01db2003-03-14 20:47:52 +0000918 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
wdenkfe8c2802002-11-03 00:38:21 +0000919}
920
921static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
922{
923 char fname[256];
924 struct stat st;
925
926 if(!d || !i) return -1;
927
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200928 strncpy(fname, (char *)d->name, d->nsize);
wdenk06d01db2003-03-14 20:47:52 +0000929 fname[d->nsize] = '\0';
wdenkfe8c2802002-11-03 00:38:21 +0000930
931 memset(&st,0,sizeof(st));
932
wdenk06d01db2003-03-14 20:47:52 +0000933 st.st_mtime = i->mtime;
934 st.st_mode = i->mode;
935 st.st_ino = i->ino;
Ilya Yanokf7384692008-11-13 19:49:31 +0300936 st.st_size = i->isize;
wdenkfe8c2802002-11-03 00:38:21 +0000937
938 dump_stat(&st, fname);
939
940 if (d->type == DT_LNK) {
941 unsigned char *src = (unsigned char *) (&i[1]);
942 putstr(" -> ");
943 putnstr(src, (int)i->dsize);
944 }
945
946 putstr("\r\n");
947
948 return 0;
949}
950
951/* list inodes with the given pino */
952static u32
953jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
954{
955 struct b_node *b;
956 struct jffs2_raw_dirent *jDir;
957
wdenk06d01db2003-03-14 20:47:52 +0000958 for (b = pL->dir.listHead; b; b = b->next) {
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200959 if (pino == b->pino) {
wdenk06d01db2003-03-14 20:47:52 +0000960 u32 i_version = 0;
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200961 int i_offset = -1;
962 struct jffs2_raw_inode *jNode = NULL;
Mark Tomlinson891224a2015-07-01 16:38:24 +1200963 struct b_node *b2;
wdenkfe8c2802002-11-03 00:38:21 +0000964
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200965 jDir = (struct jffs2_raw_dirent *)
966 get_node_mem(b->offset, pL->readbuf);
Mark Tomlinson891224a2015-07-01 16:38:24 +1200967#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
968 /* Check for more recent versions of this file */
969 int match;
970 do {
971 struct b_node *next = b->next;
972 struct jffs2_raw_dirent *jDirNext;
973 if (!next)
974 break;
975 jDirNext = (struct jffs2_raw_dirent *)
976 get_node_mem(next->offset, NULL);
977 match = jDirNext->pino == jDir->pino &&
978 jDirNext->nsize == jDir->nsize &&
979 strncmp((char *)jDirNext->name,
980 (char *)jDir->name,
981 jDir->nsize) == 0;
982 if (match) {
983 /* Use next. It is more recent */
984 b = next;
985 /* Update buffer with the new info */
986 *jDir = *jDirNext;
987 }
988 put_fl_mem(jDirNext, NULL);
989 } while (match);
990#endif
991 if (jDir->ino == 0) {
992 /* Deleted file */
993 put_fl_mem(jDir, pL->readbuf);
994 continue;
995 }
996
997 for (b2 = pL->frag.listHead; b2; b2 = b2->next) {
Petr Borsodi69dbebd2020-05-07 12:25:56 +0200998 if (b2->ino == jDir->ino &&
999 b2->version >= i_version) {
1000 i_version = b2->version;
1001 i_offset = b2->offset;
wdenk32877d62004-05-05 19:44:41 +00001002 }
wdenkfe8c2802002-11-03 00:38:21 +00001003 }
1004
Petr Borsodi69dbebd2020-05-07 12:25:56 +02001005 if (i_version >= 0) {
1006 if (jDir->type == DT_LNK)
1007 jNode = get_node_mem(i_offset, NULL);
1008 else
1009 jNode = get_fl_mem(i_offset,
1010 sizeof(*jNode),
1011 NULL);
1012 }
1013
1014 dump_inode(pL, jDir, jNode);
1015 put_fl_mem(jNode, NULL);
1016
1017 put_fl_mem(jDir, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001018 }
1019 }
1020 return pino;
1021}
1022
1023static u32
1024jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
1025{
1026 int i;
1027 char tmp[256];
1028 char working_tmp[256];
1029 char *c;
1030
1031 /* discard any leading slash */
1032 i = 0;
1033 while (fname[i] == '/')
1034 i++;
1035 strcpy(tmp, &fname[i]);
1036
1037 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
1038 {
1039 strncpy(working_tmp, tmp, c - tmp);
1040 working_tmp[c - tmp] = '\0';
1041#if 0
1042 putstr("search_inode: tmp = ");
1043 putstr(tmp);
1044 putstr("\r\n");
1045 putstr("search_inode: wtmp = ");
1046 putstr(working_tmp);
1047 putstr("\r\n");
1048 putstr("search_inode: c = ");
1049 putstr(c);
1050 putstr("\r\n");
1051#endif
1052 for (i = 0; i < strlen(c) - 1; i++)
1053 tmp[i] = c[i + 1];
1054 tmp[i] = '\0';
1055#if 0
1056 putstr("search_inode: post tmp = ");
1057 putstr(tmp);
1058 putstr("\r\n");
1059#endif
1060
1061 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
1062 putstr("find_inode failed for name=");
1063 putstr(working_tmp);
1064 putstr("\r\n");
1065 return 0;
1066 }
1067 }
1068 /* this is for the bare filename, directories have already been mapped */
1069 if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1070 putstr("find_inode failed for name=");
1071 putstr(tmp);
1072 putstr("\r\n");
1073 return 0;
1074 }
1075 return pino;
1076
1077}
1078
1079static u32
1080jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
1081{
1082 struct b_node *b;
1083 struct b_node *b2;
1084 struct jffs2_raw_dirent *jDir;
1085 struct jffs2_raw_inode *jNode;
wdenk998eaae2004-04-18 19:43:36 +00001086 u8 jDirFoundType = 0;
1087 u32 jDirFoundIno = 0;
1088 u32 jDirFoundPino = 0;
wdenkfe8c2802002-11-03 00:38:21 +00001089 char tmp[256];
1090 u32 version = 0;
1091 u32 pino;
1092 unsigned char *src;
1093
1094 /* we need to search all and return the inode with the highest version */
wdenk06d01db2003-03-14 20:47:52 +00001095 for(b = pL->dir.listHead; b; b = b->next) {
Ilya Yanok70741002008-11-13 19:49:34 +03001096 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
1097 pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001098 if (ino == jDir->ino) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001099 if (jDir->version < version) {
Ilya Yanok70741002008-11-13 19:49:34 +03001100 put_fl_mem(jDir, pL->readbuf);
wdenk7205e402003-09-10 22:30:53 +00001101 continue;
wdenk998eaae2004-04-18 19:43:36 +00001102 }
wdenkfe8c2802002-11-03 00:38:21 +00001103
wdenk998eaae2004-04-18 19:43:36 +00001104 if (jDir->version == version && jDirFoundType) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001105 /* I'm pretty sure this isn't legal */
wdenkfe8c2802002-11-03 00:38:21 +00001106 putstr(" ** ERROR ** ");
1107 putnstr(jDir->name, jDir->nsize);
1108 putLabeledWord(" has dup version (resolve) = ",
1109 version);
1110 }
1111
wdenk998eaae2004-04-18 19:43:36 +00001112 jDirFoundType = jDir->type;
1113 jDirFoundIno = jDir->ino;
1114 jDirFoundPino = jDir->pino;
wdenkfe8c2802002-11-03 00:38:21 +00001115 version = jDir->version;
1116 }
Ilya Yanok70741002008-11-13 19:49:34 +03001117 put_fl_mem(jDir, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001118 }
1119 /* now we found the right entry again. (shoulda returned inode*) */
wdenk998eaae2004-04-18 19:43:36 +00001120 if (jDirFoundType != DT_LNK)
1121 return jDirFoundIno;
wdenk06d01db2003-03-14 20:47:52 +00001122
1123 /* it's a soft link so we follow it again. */
1124 b2 = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +00001125 while (b2) {
Ilya Yanok70741002008-11-13 19:49:34 +03001126 jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset,
1127 pL->readbuf);
wdenk998eaae2004-04-18 19:43:36 +00001128 if (jNode->ino == jDirFoundIno) {
wdenk2729af92004-05-03 20:45:30 +00001129 src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
wdenkfe8c2802002-11-03 00:38:21 +00001130
1131#if 0
1132 putLabeledWord("\t\t dsize = ", jNode->dsize);
1133 putstr("\t\t target = ");
1134 putnstr(src, jNode->dsize);
1135 putstr("\r\n");
1136#endif
Wolfgang Denk77ddac92005-10-13 16:45:02 +02001137 strncpy(tmp, (char *)src, jNode->dsize);
wdenkfe8c2802002-11-03 00:38:21 +00001138 tmp[jNode->dsize] = '\0';
Ilya Yanok70741002008-11-13 19:49:34 +03001139 put_fl_mem(jNode, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001140 break;
1141 }
1142 b2 = b2->next;
Ilya Yanok70741002008-11-13 19:49:34 +03001143 put_fl_mem(jNode, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001144 }
1145 /* ok so the name of the new file to find is in tmp */
1146 /* if it starts with a slash it is root based else shared dirs */
1147 if (tmp[0] == '/')
1148 pino = 1;
1149 else
wdenk998eaae2004-04-18 19:43:36 +00001150 pino = jDirFoundPino;
wdenkfe8c2802002-11-03 00:38:21 +00001151
1152 return jffs2_1pass_search_inode(pL, tmp, pino);
1153}
1154
1155static u32
1156jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
1157{
1158 int i;
1159 char tmp[256];
1160 char working_tmp[256];
1161 char *c;
1162
1163 /* discard any leading slash */
1164 i = 0;
1165 while (fname[i] == '/')
1166 i++;
1167 strcpy(tmp, &fname[i]);
1168 working_tmp[0] = '\0';
1169 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
1170 {
1171 strncpy(working_tmp, tmp, c - tmp);
1172 working_tmp[c - tmp] = '\0';
1173 for (i = 0; i < strlen(c) - 1; i++)
1174 tmp[i] = c[i + 1];
1175 tmp[i] = '\0';
1176 /* only a failure if we arent looking at top level */
wdenk06d01db2003-03-14 20:47:52 +00001177 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
1178 (working_tmp[0])) {
wdenkfe8c2802002-11-03 00:38:21 +00001179 putstr("find_inode failed for name=");
1180 putstr(working_tmp);
1181 putstr("\r\n");
1182 return 0;
1183 }
1184 }
1185
1186 if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1187 putstr("find_inode failed for name=");
1188 putstr(tmp);
1189 putstr("\r\n");
1190 return 0;
1191 }
1192 /* this is for the bare filename, directories have already been mapped */
1193 if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
1194 putstr("find_inode failed for name=");
1195 putstr(tmp);
1196 putstr("\r\n");
1197 return 0;
1198 }
1199 return pino;
1200
1201}
1202
1203unsigned char
1204jffs2_1pass_rescan_needed(struct part_info *part)
1205{
1206 struct b_node *b;
wdenk998eaae2004-04-18 19:43:36 +00001207 struct jffs2_unknown_node onode;
wdenkfe8c2802002-11-03 00:38:21 +00001208 struct jffs2_unknown_node *node;
wdenk06d01db2003-03-14 20:47:52 +00001209 struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
wdenkfe8c2802002-11-03 00:38:21 +00001210
1211 if (part->jffs2_priv == 0){
1212 DEBUGF ("rescan: First time in use\n");
1213 return 1;
1214 }
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001215
wdenkfe8c2802002-11-03 00:38:21 +00001216 /* if we have no list, we need to rescan */
wdenk06d01db2003-03-14 20:47:52 +00001217 if (pL->frag.listCount == 0) {
wdenkfe8c2802002-11-03 00:38:21 +00001218 DEBUGF ("rescan: fraglist zero\n");
1219 return 1;
1220 }
1221
wdenk06d01db2003-03-14 20:47:52 +00001222 /* but suppose someone reflashed a partition at the same offset... */
1223 b = pL->dir.listHead;
wdenkfe8c2802002-11-03 00:38:21 +00001224 while (b) {
wdenk998eaae2004-04-18 19:43:36 +00001225 node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
1226 sizeof(onode), &onode);
wdenkfe8c2802002-11-03 00:38:21 +00001227 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
wdenk06d01db2003-03-14 20:47:52 +00001228 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
1229 (unsigned long) b->offset);
wdenkfe8c2802002-11-03 00:38:21 +00001230 return 1;
1231 }
1232 b = b->next;
1233 }
1234 return 0;
1235}
1236
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001237#ifdef CONFIG_JFFS2_SUMMARY
1238static u32 sum_get_unaligned32(u32 *ptr)
1239{
1240 u32 val;
1241 u8 *p = (u8 *)ptr;
1242
1243 val = *p | (*(p + 1) << 8) | (*(p + 2) << 16) | (*(p + 3) << 24);
1244
1245 return __le32_to_cpu(val);
1246}
1247
1248static u16 sum_get_unaligned16(u16 *ptr)
1249{
1250 u16 val;
1251 u8 *p = (u8 *)ptr;
1252
1253 val = *p | (*(p + 1) << 8);
1254
1255 return __le16_to_cpu(val);
1256}
1257
Ilya Yanok9b707622008-11-13 19:49:35 +03001258#define dbg_summary(...) do {} while (0);
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001259/*
1260 * Process the stored summary information - helper function for
Ilya Yanok9b707622008-11-13 19:49:35 +03001261 * jffs2_sum_scan_sumnode()
1262 */
1263
1264static int jffs2_sum_process_sum_data(struct part_info *part, uint32_t offset,
1265 struct jffs2_raw_summary *summary,
1266 struct b_lists *pL)
1267{
1268 void *sp;
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001269 int i, pass;
Petr Borsodi69dbebd2020-05-07 12:25:56 +02001270 struct b_node *b;
Ilya Yanok9b707622008-11-13 19:49:35 +03001271
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001272 for (pass = 0; pass < 2; pass++) {
1273 sp = summary->sum;
Ilya Yanok9b707622008-11-13 19:49:35 +03001274
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001275 for (i = 0; i < summary->sum_num; i++) {
1276 struct jffs2_sum_unknown_flash *spu = sp;
1277 dbg_summary("processing summary index %d\n", i);
Ilya Yanok9b707622008-11-13 19:49:35 +03001278
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001279 switch (sum_get_unaligned16(&spu->nodetype)) {
1280 case JFFS2_NODETYPE_INODE: {
Ilya Yanok9b707622008-11-13 19:49:35 +03001281 struct jffs2_sum_inode_flash *spi;
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001282 if (pass) {
1283 spi = sp;
Ilya Yanok9b707622008-11-13 19:49:35 +03001284
Petr Borsodi69dbebd2020-05-07 12:25:56 +02001285 b = insert_node(&pL->frag);
1286 if (!b)
1287 return -1;
1288 b->offset = (u32)part->offset +
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001289 offset +
1290 sum_get_unaligned32(
Petr Borsodi69dbebd2020-05-07 12:25:56 +02001291 &spi->offset);
1292 b->version = sum_get_unaligned32(
1293 &spi->version);
1294 b->ino = sum_get_unaligned32(
1295 &spi->inode);
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001296 }
Ilya Yanok9b707622008-11-13 19:49:35 +03001297
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001298 sp += JFFS2_SUMMARY_INODE_SIZE;
Ilya Yanok9b707622008-11-13 19:49:35 +03001299
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001300 break;
1301 }
1302 case JFFS2_NODETYPE_DIRENT: {
1303 struct jffs2_sum_dirent_flash *spd;
1304 spd = sp;
1305 if (pass) {
Petr Borsodi69dbebd2020-05-07 12:25:56 +02001306 b = insert_node(&pL->dir);
1307 if (!b)
1308 return -1;
1309 b->offset = (u32)part->offset +
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001310 offset +
1311 sum_get_unaligned32(
Petr Borsodi69dbebd2020-05-07 12:25:56 +02001312 &spd->offset);
1313 b->version = sum_get_unaligned32(
1314 &spd->version);
1315 b->pino = sum_get_unaligned32(
1316 &spd->pino);
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001317 }
Ilya Yanok9b707622008-11-13 19:49:35 +03001318
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001319 sp += JFFS2_SUMMARY_DIRENT_SIZE(
1320 spd->nsize);
Ilya Yanok9b707622008-11-13 19:49:35 +03001321
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001322 break;
1323 }
1324 default : {
1325 uint16_t nodetype = sum_get_unaligned16(
1326 &spu->nodetype);
1327 printf("Unsupported node type %x found"
1328 " in summary!\n",
1329 nodetype);
1330 if ((nodetype & JFFS2_COMPAT_MASK) ==
1331 JFFS2_FEATURE_INCOMPAT)
1332 return -EIO;
1333 return -EBADMSG;
1334 }
Ilya Yanok9b707622008-11-13 19:49:35 +03001335 }
1336 }
1337 }
1338 return 0;
1339}
1340
1341/* Process the summary node - called from jffs2_scan_eraseblock() */
1342int jffs2_sum_scan_sumnode(struct part_info *part, uint32_t offset,
1343 struct jffs2_raw_summary *summary, uint32_t sumsize,
1344 struct b_lists *pL)
1345{
1346 struct jffs2_unknown_node crcnode;
Tom Rini1c8fdf82016-03-27 14:48:36 -04001347 int ret, __maybe_unused ofs;
Ilya Yanok9b707622008-11-13 19:49:35 +03001348 uint32_t crc;
1349
1350 ofs = part->sector_size - sumsize;
1351
1352 dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
1353 offset, offset + ofs, sumsize);
1354
1355 /* OK, now check for node validity and CRC */
1356 crcnode.magic = JFFS2_MAGIC_BITMASK;
1357 crcnode.nodetype = JFFS2_NODETYPE_SUMMARY;
1358 crcnode.totlen = summary->totlen;
1359 crc = crc32_no_comp(0, (uchar *)&crcnode, sizeof(crcnode)-4);
1360
1361 if (summary->hdr_crc != crc) {
1362 dbg_summary("Summary node header is corrupt (bad CRC or "
1363 "no summary at all)\n");
1364 goto crc_err;
1365 }
1366
1367 if (summary->totlen != sumsize) {
1368 dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
1369 goto crc_err;
1370 }
1371
1372 crc = crc32_no_comp(0, (uchar *)summary,
1373 sizeof(struct jffs2_raw_summary)-8);
1374
1375 if (summary->node_crc != crc) {
1376 dbg_summary("Summary node is corrupt (bad CRC)\n");
1377 goto crc_err;
1378 }
1379
1380 crc = crc32_no_comp(0, (uchar *)summary->sum,
1381 sumsize - sizeof(struct jffs2_raw_summary));
1382
1383 if (summary->sum_crc != crc) {
1384 dbg_summary("Summary node data is corrupt (bad CRC)\n");
1385 goto crc_err;
1386 }
1387
1388 if (summary->cln_mkr)
1389 dbg_summary("Summary : CLEANMARKER node \n");
1390
1391 ret = jffs2_sum_process_sum_data(part, offset, summary, pL);
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001392 if (ret == -EBADMSG)
1393 return 0;
Ilya Yanok9b707622008-11-13 19:49:35 +03001394 if (ret)
1395 return ret; /* real error */
1396
1397 return 1;
1398
1399crc_err:
1400 putstr("Summary node crc error, skipping summary information.\n");
1401
1402 return 0;
1403}
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001404#endif /* CONFIG_JFFS2_SUMMARY */
Ilya Yanok9b707622008-11-13 19:49:35 +03001405
wdenk06d01db2003-03-14 20:47:52 +00001406#ifdef DEBUG_FRAGMENTS
1407static void
1408dump_fragments(struct b_lists *pL)
1409{
1410 struct b_node *b;
wdenk998eaae2004-04-18 19:43:36 +00001411 struct jffs2_raw_inode ojNode;
wdenk06d01db2003-03-14 20:47:52 +00001412 struct jffs2_raw_inode *jNode;
1413
1414 putstr("\r\n\r\n******The fragment Entries******\r\n");
1415 b = pL->frag.listHead;
1416 while (b) {
wdenk998eaae2004-04-18 19:43:36 +00001417 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1418 sizeof(ojNode), &ojNode);
wdenk06d01db2003-03-14 20:47:52 +00001419 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
1420 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
1421 putLabeledWord("\tbuild_list: inode = ", jNode->ino);
1422 putLabeledWord("\tbuild_list: version = ", jNode->version);
1423 putLabeledWord("\tbuild_list: isize = ", jNode->isize);
1424 putLabeledWord("\tbuild_list: atime = ", jNode->atime);
1425 putLabeledWord("\tbuild_list: offset = ", jNode->offset);
1426 putLabeledWord("\tbuild_list: csize = ", jNode->csize);
1427 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
1428 putLabeledWord("\tbuild_list: compr = ", jNode->compr);
1429 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
1430 putLabeledWord("\tbuild_list: flags = ", jNode->flags);
wdenk8bde7f72003-06-27 21:31:46 +00001431 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
wdenk06d01db2003-03-14 20:47:52 +00001432 b = b->next;
1433 }
1434}
1435#endif
1436
1437#ifdef DEBUG_DIRENTS
1438static void
1439dump_dirents(struct b_lists *pL)
1440{
1441 struct b_node *b;
1442 struct jffs2_raw_dirent *jDir;
1443
1444 putstr("\r\n\r\n******The directory Entries******\r\n");
1445 b = pL->dir.listHead;
1446 while (b) {
Ilya Yanok70741002008-11-13 19:49:34 +03001447 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
1448 pL->readbuf);
wdenk06d01db2003-03-14 20:47:52 +00001449 putstr("\r\n");
1450 putnstr(jDir->name, jDir->nsize);
1451 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
1452 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
1453 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
1454 putLabeledWord("\tbuild_list: pino = ", jDir->pino);
1455 putLabeledWord("\tbuild_list: version = ", jDir->version);
1456 putLabeledWord("\tbuild_list: ino = ", jDir->ino);
1457 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
1458 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
1459 putLabeledWord("\tbuild_list: type = ", jDir->type);
1460 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
1461 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001462 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
wdenk06d01db2003-03-14 20:47:52 +00001463 b = b->next;
Ilya Yanok70741002008-11-13 19:49:34 +03001464 put_fl_mem(jDir, pL->readbuf);
wdenk06d01db2003-03-14 20:47:52 +00001465 }
1466}
1467#endif
1468
Mark Tomlinson081adef2015-07-01 16:38:27 +12001469#define DEFAULT_EMPTY_SCAN_SIZE 256
Ilya Yanok8a36d312008-11-13 19:49:33 +03001470
1471static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size)
1472{
1473 if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
1474 return sector_size;
1475 else
1476 return DEFAULT_EMPTY_SCAN_SIZE;
1477}
1478
wdenkfe8c2802002-11-03 00:38:21 +00001479static u32
1480jffs2_1pass_build_lists(struct part_info * part)
1481{
1482 struct b_lists *pL;
Petr Borsodi25ec2282020-05-07 12:25:55 +02001483 union jffs2_node_union *node;
Tom Rini18e86722013-12-05 14:48:38 -05001484 u32 nr_sectors;
Ilya Yanok8a36d312008-11-13 19:49:33 +03001485 u32 i;
wdenkfe8c2802002-11-03 00:38:21 +00001486 u32 counter4 = 0;
1487 u32 counterF = 0;
1488 u32 counterN = 0;
Ilya Yanok70741002008-11-13 19:49:34 +03001489 u32 max_totlen = 0;
Mark Tomlinsonc5b19402015-07-01 16:38:26 +12001490 u32 buf_size;
Ilya Yanok8a36d312008-11-13 19:49:33 +03001491 char *buf;
wdenkfe8c2802002-11-03 00:38:21 +00001492
Tom Rini18e86722013-12-05 14:48:38 -05001493 nr_sectors = lldiv(part->size, part->sector_size);
wdenkfe8c2802002-11-03 00:38:21 +00001494 /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
1495 /* jffs2 list building enterprise nope. in newer versions the overhead is */
1496 /* only about 5 %. not enough to inconvenience people for. */
1497 /* lcd_off(); */
1498
1499 /* if we are building a list we need to refresh the cache. */
wdenkfe8c2802002-11-03 00:38:21 +00001500 jffs_init_1pass_list(part);
wdenk06d01db2003-03-14 20:47:52 +00001501 pL = (struct b_lists *)part->jffs2_priv;
Mark Tomlinsonc5b19402015-07-01 16:38:26 +12001502 buf = malloc(DEFAULT_EMPTY_SCAN_SIZE);
wdenk4b9206e2004-03-23 22:14:11 +00001503 puts ("Scanning JFFS2 FS: ");
wdenkfe8c2802002-11-03 00:38:21 +00001504
1505 /* start at the beginning of the partition */
Ilya Yanok8a36d312008-11-13 19:49:33 +03001506 for (i = 0; i < nr_sectors; i++) {
1507 uint32_t sector_ofs = i * part->sector_size;
1508 uint32_t buf_ofs = sector_ofs;
Ilya Yanok9b707622008-11-13 19:49:35 +03001509 uint32_t buf_len;
Ilya Yanok8a36d312008-11-13 19:49:33 +03001510 uint32_t ofs, prevofs;
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001511#ifdef CONFIG_JFFS2_SUMMARY
Ilya Yanok9b707622008-11-13 19:49:35 +03001512 struct jffs2_sum_marker *sm;
1513 void *sumptr = NULL;
1514 uint32_t sumlen;
1515 int ret;
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001516#endif
Mark Tomlinson54a88382015-07-01 16:38:28 +12001517 /* Indicates a sector with a CLEANMARKER was found */
1518 int clean_sector = 0;
Petr Borsodi25ec2282020-05-07 12:25:55 +02001519 struct jffs2_unknown_node crcnode;
Petr Borsodi69dbebd2020-05-07 12:25:56 +02001520 struct b_node *b;
wdenk0b8fa032004-04-25 14:37:29 +00001521
Mark Tomlinsonc5b19402015-07-01 16:38:26 +12001522 /* Set buf_size to maximum length */
1523 buf_size = DEFAULT_EMPTY_SCAN_SIZE;
Stuart Wood86d32732008-06-02 16:40:08 -04001524 WATCHDOG_RESET();
Ilya Yanok9b707622008-11-13 19:49:35 +03001525
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001526#ifdef CONFIG_JFFS2_SUMMARY
Ilya Yanok9b707622008-11-13 19:49:35 +03001527 buf_len = sizeof(*sm);
1528
1529 /* Read as much as we want into the _end_ of the preallocated
1530 * buffer
1531 */
1532 get_fl_mem(part->offset + sector_ofs + part->sector_size -
1533 buf_len, buf_len, buf + buf_size - buf_len);
1534
1535 sm = (void *)buf + buf_size - sizeof(*sm);
1536 if (sm->magic == JFFS2_SUM_MAGIC) {
1537 sumlen = part->sector_size - sm->offset;
1538 sumptr = buf + buf_size - sumlen;
1539
1540 /* Now, make sure the summary itself is available */
1541 if (sumlen > buf_size) {
1542 /* Need to kmalloc for this. */
1543 sumptr = malloc(sumlen);
1544 if (!sumptr) {
1545 putstr("Can't get memory for summary "
1546 "node!\n");
Ilya Yanokb6440062009-08-12 16:42:48 +04001547 free(buf);
1548 jffs2_free_cache(part);
Ilya Yanok9b707622008-11-13 19:49:35 +03001549 return 0;
1550 }
1551 memcpy(sumptr + sumlen - buf_len, buf +
1552 buf_size - buf_len, buf_len);
1553 }
1554 if (buf_len < sumlen) {
1555 /* Need to read more so that the entire summary
1556 * node is present
1557 */
1558 get_fl_mem(part->offset + sector_ofs +
1559 part->sector_size - sumlen,
1560 sumlen - buf_len, sumptr);
1561 }
1562 }
1563
1564 if (sumptr) {
1565 ret = jffs2_sum_scan_sumnode(part, sector_ofs, sumptr,
1566 sumlen, pL);
1567
1568 if (buf_size && sumlen > buf_size)
1569 free(sumptr);
Ilya Yanokb6440062009-08-12 16:42:48 +04001570 if (ret < 0) {
1571 free(buf);
1572 jffs2_free_cache(part);
Ilya Yanok9b707622008-11-13 19:49:35 +03001573 return 0;
Ilya Yanokb6440062009-08-12 16:42:48 +04001574 }
Ilya Yanok9b707622008-11-13 19:49:35 +03001575 if (ret)
1576 continue;
1577
1578 }
Ilya Yanok8cf19b92009-07-17 15:02:42 +04001579#endif /* CONFIG_JFFS2_SUMMARY */
Ilya Yanok9b707622008-11-13 19:49:35 +03001580
1581 buf_len = EMPTY_SCAN_SIZE(part->sector_size);
1582
Ilya Yanok8a36d312008-11-13 19:49:33 +03001583 get_fl_mem((u32)part->offset + buf_ofs, buf_len, buf);
Stuart Wood86d32732008-06-02 16:40:08 -04001584
Ilya Yanok8a36d312008-11-13 19:49:33 +03001585 /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
1586 ofs = 0;
1587
1588 /* Scan only 4KiB of 0xFF before declaring it's empty */
1589 while (ofs < EMPTY_SCAN_SIZE(part->sector_size) &&
1590 *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
1591 ofs += 4;
1592
1593 if (ofs == EMPTY_SCAN_SIZE(part->sector_size))
1594 continue;
1595
1596 ofs += sector_ofs;
1597 prevofs = ofs - 1;
Mark Tomlinsonc5b19402015-07-01 16:38:26 +12001598 /*
1599 * Set buf_size down to the minimum size required.
1600 * This prevents reading in chunks of flash data unnecessarily.
1601 */
1602 buf_size = sizeof(union jffs2_node_union);
Ilya Yanok8a36d312008-11-13 19:49:33 +03001603
1604 scan_more:
1605 while (ofs < sector_ofs + part->sector_size) {
1606 if (ofs == prevofs) {
1607 printf("offset %08x already seen, skip\n", ofs);
1608 ofs += 4;
1609 counter4++;
1610 continue;
1611 }
1612 prevofs = ofs;
1613 if (sector_ofs + part->sector_size <
Petr Borsodi25ec2282020-05-07 12:25:55 +02001614 ofs + sizeof(struct jffs2_unknown_node))
Ilya Yanok8a36d312008-11-13 19:49:33 +03001615 break;
Petr Borsodi25ec2282020-05-07 12:25:55 +02001616 if (buf_ofs + buf_len <
1617 ofs + sizeof(struct jffs2_unknown_node)) {
Ilya Yanok8a36d312008-11-13 19:49:33 +03001618 buf_len = min_t(uint32_t, buf_size, sector_ofs
1619 + part->sector_size - ofs);
1620 get_fl_mem((u32)part->offset + ofs, buf_len,
1621 buf);
1622 buf_ofs = ofs;
1623 }
1624
Petr Borsodi25ec2282020-05-07 12:25:55 +02001625 node = (union jffs2_node_union *)&buf[ofs - buf_ofs];
Ilya Yanok8a36d312008-11-13 19:49:33 +03001626
1627 if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
1628 uint32_t inbuf_ofs;
Wolfgang Denk16b9afd2011-10-05 22:58:11 +02001629 uint32_t scan_end;
Ilya Yanok8a36d312008-11-13 19:49:33 +03001630
Ilya Yanok8a36d312008-11-13 19:49:33 +03001631 ofs += 4;
1632 scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(
1633 part->sector_size)/8,
1634 buf_len);
1635 more_empty:
1636 inbuf_ofs = ofs - buf_ofs;
1637 while (inbuf_ofs < scan_end) {
1638 if (*(uint32_t *)(&buf[inbuf_ofs]) !=
1639 0xffffffff)
1640 goto scan_more;
1641
1642 inbuf_ofs += 4;
1643 ofs += 4;
wdenk998eaae2004-04-18 19:43:36 +00001644 }
Ilya Yanok8a36d312008-11-13 19:49:33 +03001645 /* Ran off end. */
Mark Tomlinson54a88382015-07-01 16:38:28 +12001646 /*
1647 * If this sector had a clean marker at the
1648 * beginning, and immediately following this
1649 * have been a bunch of FF bytes, treat the
1650 * entire sector as empty.
1651 */
1652 if (clean_sector)
1653 break;
Ilya Yanok8a36d312008-11-13 19:49:33 +03001654
1655 /* See how much more there is to read in this
1656 * eraseblock...
1657 */
1658 buf_len = min_t(uint32_t, buf_size,
1659 sector_ofs +
1660 part->sector_size - ofs);
1661 if (!buf_len) {
1662 /* No more to read. Break out of main
1663 * loop without marking this range of
1664 * empty space as dirty (because it's
1665 * not)
1666 */
1667 break;
1668 }
1669 scan_end = buf_len;
1670 get_fl_mem((u32)part->offset + ofs, buf_len,
1671 buf);
1672 buf_ofs = ofs;
1673 goto more_empty;
1674 }
Mark Tomlinson54a88382015-07-01 16:38:28 +12001675 /*
1676 * Found something not erased in the sector, so reset
1677 * the 'clean_sector' flag.
1678 */
1679 clean_sector = 0;
Petr Borsodi25ec2282020-05-07 12:25:55 +02001680 if (node->u.magic != JFFS2_MAGIC_BITMASK) {
Ilya Yanok8a36d312008-11-13 19:49:33 +03001681 ofs += 4;
1682 counter4++;
1683 continue;
1684 }
Petr Borsodi25ec2282020-05-07 12:25:55 +02001685
1686 crcnode.magic = node->u.magic;
1687 crcnode.nodetype = node->u.nodetype | JFFS2_NODE_ACCURATE;
1688 crcnode.totlen = node->u.totlen;
1689 crcnode.hdr_crc = node->u.hdr_crc;
1690 if (!hdr_crc(&crcnode)) {
Ilya Yanok8a36d312008-11-13 19:49:33 +03001691 ofs += 4;
1692 counter4++;
1693 continue;
1694 }
Petr Borsodi25ec2282020-05-07 12:25:55 +02001695
1696 if (ofs + node->u.totlen > sector_ofs + part->sector_size) {
1697 ofs += 4;
1698 counter4++;
1699 continue;
1700 }
1701
1702 if (!(node->u.nodetype & JFFS2_NODE_ACCURATE)) {
1703 DEBUGF("Obsolete node type: %x len %d offset 0x%x\n",
1704 node->u.nodetype, node->u.totlen, ofs);
1705 ofs += ((node->u.totlen + 3) & ~3);
1706 counterF++;
1707 continue;
1708 }
1709
Ilya Yanok8a36d312008-11-13 19:49:33 +03001710 /* if its a fragment add it */
Petr Borsodi25ec2282020-05-07 12:25:55 +02001711 switch (node->u.nodetype) {
Ilya Yanok8a36d312008-11-13 19:49:33 +03001712 case JFFS2_NODETYPE_INODE:
Petr Borsodi25ec2282020-05-07 12:25:55 +02001713 if (buf_ofs + buf_len <
1714 ofs + sizeof(struct jffs2_raw_inode)) {
Mark Tomlinsonc5b19402015-07-01 16:38:26 +12001715 buf_len = min_t(uint32_t,
1716 sizeof(struct jffs2_raw_inode),
1717 sector_ofs +
1718 part->sector_size -
1719 ofs);
Ilya Yanok8a36d312008-11-13 19:49:33 +03001720 get_fl_mem((u32)part->offset + ofs,
1721 buf_len, buf);
1722 buf_ofs = ofs;
1723 node = (void *)buf;
1724 }
Mark Tomlinsonc5b19402015-07-01 16:38:26 +12001725 if (!inode_crc((struct jffs2_raw_inode *)node))
1726 break;
Ilya Yanok8a36d312008-11-13 19:49:33 +03001727
Petr Borsodi69dbebd2020-05-07 12:25:56 +02001728 b = insert_node(&pL->frag);
1729 if (!b) {
Ilya Yanokb6440062009-08-12 16:42:48 +04001730 free(buf);
1731 jffs2_free_cache(part);
Ilya Yanok8a36d312008-11-13 19:49:33 +03001732 return 0;
Ilya Yanokb6440062009-08-12 16:42:48 +04001733 }
Petr Borsodi69dbebd2020-05-07 12:25:56 +02001734 b->offset = (u32)part->offset + ofs;
1735 b->version = node->i.version;
1736 b->ino = node->i.ino;
Petr Borsodi25ec2282020-05-07 12:25:55 +02001737 if (max_totlen < node->u.totlen)
1738 max_totlen = node->u.totlen;
Ilya Yanok8a36d312008-11-13 19:49:33 +03001739 break;
1740 case JFFS2_NODETYPE_DIRENT:
1741 if (buf_ofs + buf_len < ofs + sizeof(struct
1742 jffs2_raw_dirent) +
1743 ((struct
1744 jffs2_raw_dirent *)
1745 node)->nsize) {
Mark Tomlinsonc5b19402015-07-01 16:38:26 +12001746 buf_len = min_t(uint32_t,
Petr Borsodi25ec2282020-05-07 12:25:55 +02001747 node->u.totlen,
Mark Tomlinsonc5b19402015-07-01 16:38:26 +12001748 sector_ofs +
1749 part->sector_size -
1750 ofs);
Ilya Yanok8a36d312008-11-13 19:49:33 +03001751 get_fl_mem((u32)part->offset + ofs,
1752 buf_len, buf);
1753 buf_ofs = ofs;
1754 node = (void *)buf;
1755 }
1756
1757 if (!dirent_crc((struct jffs2_raw_dirent *)
1758 node) ||
1759 !dirent_name_crc(
1760 (struct
1761 jffs2_raw_dirent *)
1762 node))
1763 break;
wdenkfc1cfcd2004-04-25 15:41:35 +00001764 if (! (counterN%100))
wdenk4b9206e2004-03-23 22:14:11 +00001765 puts ("\b\b. ");
Petr Borsodi69dbebd2020-05-07 12:25:56 +02001766 b = insert_node(&pL->dir);
1767 if (!b) {
Ilya Yanokb6440062009-08-12 16:42:48 +04001768 free(buf);
1769 jffs2_free_cache(part);
wdenkfe8c2802002-11-03 00:38:21 +00001770 return 0;
Ilya Yanokb6440062009-08-12 16:42:48 +04001771 }
Petr Borsodi69dbebd2020-05-07 12:25:56 +02001772 b->offset = (u32)part->offset + ofs;
1773 b->version = node->d.version;
1774 b->pino = node->d.pino;
Petr Borsodi25ec2282020-05-07 12:25:55 +02001775 if (max_totlen < node->u.totlen)
1776 max_totlen = node->u.totlen;
wdenkfe8c2802002-11-03 00:38:21 +00001777 counterN++;
Ilya Yanok8a36d312008-11-13 19:49:33 +03001778 break;
1779 case JFFS2_NODETYPE_CLEANMARKER:
Petr Borsodi25ec2282020-05-07 12:25:55 +02001780 if (node->u.totlen != sizeof(struct jffs2_unknown_node))
wdenk06d01db2003-03-14 20:47:52 +00001781 printf("OOPS Cleanmarker has bad size "
Wolfgang Denkb64f1902008-07-14 15:06:35 +02001782 "%d != %zu\n",
Petr Borsodi25ec2282020-05-07 12:25:55 +02001783 node->u.totlen,
wdenk06d01db2003-03-14 20:47:52 +00001784 sizeof(struct jffs2_unknown_node));
Petr Borsodi25ec2282020-05-07 12:25:55 +02001785 if (node->u.totlen ==
1786 sizeof(struct jffs2_unknown_node) &&
1787 ofs == sector_ofs) {
Mark Tomlinson54a88382015-07-01 16:38:28 +12001788 /*
1789 * Found a CLEANMARKER at the beginning
1790 * of the sector. It's in the correct
1791 * place with correct size and CRC.
1792 */
1793 clean_sector = 1;
1794 }
Ilya Yanok8a36d312008-11-13 19:49:33 +03001795 break;
1796 case JFFS2_NODETYPE_PADDING:
Petr Borsodi25ec2282020-05-07 12:25:55 +02001797 if (node->u.totlen <
1798 sizeof(struct jffs2_unknown_node))
wdenk7205e402003-09-10 22:30:53 +00001799 printf("OOPS Padding has bad size "
Wolfgang Denkb64f1902008-07-14 15:06:35 +02001800 "%d < %zu\n",
Petr Borsodi25ec2282020-05-07 12:25:55 +02001801 node->u.totlen,
wdenk7205e402003-09-10 22:30:53 +00001802 sizeof(struct jffs2_unknown_node));
Ilya Yanok8a36d312008-11-13 19:49:33 +03001803 break;
Ilya Yanok9b707622008-11-13 19:49:35 +03001804 case JFFS2_NODETYPE_SUMMARY:
1805 break;
Ilya Yanok8a36d312008-11-13 19:49:33 +03001806 default:
Wolfgang Denkb64f1902008-07-14 15:06:35 +02001807 printf("Unknown node type: %x len %d offset 0x%x\n",
Petr Borsodi25ec2282020-05-07 12:25:55 +02001808 node->u.nodetype,
1809 node->u.totlen, ofs);
wdenkfe8c2802002-11-03 00:38:21 +00001810 }
Petr Borsodi25ec2282020-05-07 12:25:55 +02001811 ofs += ((node->u.totlen + 3) & ~3);
wdenkfe8c2802002-11-03 00:38:21 +00001812 counterF++;
wdenkfe8c2802002-11-03 00:38:21 +00001813 }
wdenkfe8c2802002-11-03 00:38:21 +00001814 }
1815
Ilya Yanok8a36d312008-11-13 19:49:33 +03001816 free(buf);
Mark Tomlinson10d3ac32015-07-01 16:38:29 +12001817#if defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS)
1818 /*
1819 * Sort the lists.
1820 */
1821 sort_list(&pL->frag);
1822 sort_list(&pL->dir);
1823#endif
wdenkfe8c2802002-11-03 00:38:21 +00001824 putstr("\b\b done.\r\n"); /* close off the dots */
Ilya Yanok70741002008-11-13 19:49:34 +03001825
1826 /* We don't care if malloc failed - then each read operation will
1827 * allocate its own buffer as necessary (NAND) or will read directly
1828 * from flash (NOR).
1829 */
1830 pL->readbuf = malloc(max_totlen);
1831
wdenkfe8c2802002-11-03 00:38:21 +00001832 /* turn the lcd back on. */
1833 /* splash(); */
1834
1835#if 0
wdenk06d01db2003-03-14 20:47:52 +00001836 putLabeledWord("dir entries = ", pL->dir.listCount);
1837 putLabeledWord("frag entries = ", pL->frag.listCount);
wdenkfe8c2802002-11-03 00:38:21 +00001838 putLabeledWord("+4 increments = ", counter4);
1839 putLabeledWord("+file_offset increments = ", counterF);
1840
1841#endif
1842
wdenk06d01db2003-03-14 20:47:52 +00001843#ifdef DEBUG_DIRENTS
1844 dump_dirents(pL);
1845#endif
wdenkfe8c2802002-11-03 00:38:21 +00001846
wdenk06d01db2003-03-14 20:47:52 +00001847#ifdef DEBUG_FRAGMENTS
1848 dump_fragments(pL);
1849#endif
wdenkfe8c2802002-11-03 00:38:21 +00001850
wdenkfe8c2802002-11-03 00:38:21 +00001851 /* give visual feedback that we are done scanning the flash */
1852 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
1853 return 1;
1854}
1855
1856
wdenkfe8c2802002-11-03 00:38:21 +00001857static u32
1858jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
1859{
1860 struct b_node *b;
wdenk998eaae2004-04-18 19:43:36 +00001861 struct jffs2_raw_inode ojNode;
wdenkfe8c2802002-11-03 00:38:21 +00001862 struct jffs2_raw_inode *jNode;
1863 int i;
1864
wdenkfe8c2802002-11-03 00:38:21 +00001865 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1866 piL->compr_info[i].num_frags = 0;
1867 piL->compr_info[i].compr_sum = 0;
1868 piL->compr_info[i].decompr_sum = 0;
1869 }
1870
wdenk06d01db2003-03-14 20:47:52 +00001871 b = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +00001872 while (b) {
wdenk998eaae2004-04-18 19:43:36 +00001873 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1874 sizeof(ojNode), &ojNode);
wdenkfe8c2802002-11-03 00:38:21 +00001875 if (jNode->compr < JFFS2_NUM_COMPR) {
1876 piL->compr_info[jNode->compr].num_frags++;
1877 piL->compr_info[jNode->compr].compr_sum += jNode->csize;
1878 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
1879 }
1880 b = b->next;
1881 }
1882 return 0;
1883}
1884
1885
wdenkfe8c2802002-11-03 00:38:21 +00001886static struct b_lists *
1887jffs2_get_list(struct part_info * part, const char *who)
1888{
Wolfgang Denk700a0c62005-08-08 01:03:24 +02001889 /* copy requested part_info struct pointer to global location */
1890 current_part = part;
1891
wdenkfe8c2802002-11-03 00:38:21 +00001892 if (jffs2_1pass_rescan_needed(part)) {
1893 if (!jffs2_1pass_build_lists(part)) {
1894 printf("%s: Failed to scan JFFSv2 file structure\n", who);
1895 return NULL;
1896 }
1897 }
1898 return (struct b_lists *)part->jffs2_priv;
1899}
1900
1901
1902/* Print directory / file contents */
1903u32
1904jffs2_1pass_ls(struct part_info * part, const char *fname)
1905{
1906 struct b_lists *pl;
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001907 long ret = 1;
wdenkfe8c2802002-11-03 00:38:21 +00001908 u32 inode;
1909
wdenk06d01db2003-03-14 20:47:52 +00001910 if (! (pl = jffs2_get_list(part, "ls")))
wdenkfe8c2802002-11-03 00:38:21 +00001911 return 0;
1912
1913 if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
1914 putstr("ls: Failed to scan jffs2 file structure\r\n");
1915 return 0;
1916 }
wdenkfc1cfcd2004-04-25 15:41:35 +00001917
1918
wdenkfe8c2802002-11-03 00:38:21 +00001919#if 0
1920 putLabeledWord("found file at inode = ", inode);
1921 putLabeledWord("read_inode returns = ", ret);
1922#endif
wdenkfc1cfcd2004-04-25 15:41:35 +00001923
1924 return ret;
wdenkfe8c2802002-11-03 00:38:21 +00001925}
1926
1927
wdenkfe8c2802002-11-03 00:38:21 +00001928/* Load a file from flash into memory. fname can be a full path */
1929u32
1930jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
1931{
1932
1933 struct b_lists *pl;
Wolfgang Denk87b8bd52005-08-16 09:32:45 +02001934 long ret = 1;
wdenkfe8c2802002-11-03 00:38:21 +00001935 u32 inode;
1936
1937 if (! (pl = jffs2_get_list(part, "load")))
1938 return 0;
1939
1940 if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
1941 putstr("load: Failed to find inode\r\n");
1942 return 0;
1943 }
1944
1945 /* Resolve symlinks */
1946 if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
1947 putstr("load: Failed to resolve inode structure\r\n");
1948 return 0;
1949 }
1950
1951 if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
1952 putstr("load: Failed to read inode\r\n");
1953 return 0;
1954 }
1955
1956 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1957 (unsigned long) dest, ret);
1958 return ret;
1959}
1960
1961/* Return information about the fs on this partition */
1962u32
1963jffs2_1pass_info(struct part_info * part)
1964{
1965 struct b_jffs2_info info;
1966 struct b_lists *pl;
1967 int i;
1968
1969 if (! (pl = jffs2_get_list(part, "info")))
1970 return 0;
1971
1972 jffs2_1pass_fill_info(pl, &info);
wdenk06d01db2003-03-14 20:47:52 +00001973 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
wdenk4b9206e2004-03-23 22:14:11 +00001974 printf ("Compression: %s\n"
1975 "\tfrag count: %d\n"
1976 "\tcompressed sum: %d\n"
1977 "\tuncompressed sum: %d\n",
1978 compr_names[i],
1979 info.compr_info[i].num_frags,
1980 info.compr_info[i].compr_sum,
1981 info.compr_info[i].decompr_sum);
wdenkfe8c2802002-11-03 00:38:21 +00001982 }
1983 return 1;
1984}