blob: b5f74d65017a08615db25762b41296a172bc9563 [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 Denka1be4762008-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
wdenk6b58f332003-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-VILLARD03836942008-10-16 15:01:15 +020095 * The fragment sorting feature must be enabled by CONFIG_SYS_JFFS2_SORT_FRAGMENTS.
wdenk6b58f332003-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
wdenkae65f502004-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!
wdenk6b58f332003-03-14 20:47:52 +0000111 */
112
wdenkfe8c2802002-11-03 00:38:21 +0000113#include <config.h>
114#include <malloc.h>
Tom Rini3cd84d62013-12-05 14:48:38 -0500115#include <div64.h>
Tom Rini7c4734d2016-03-27 14:48:36 -0400116#include <linux/compiler.h>
wdenkfe8c2802002-11-03 00:38:21 +0000117#include <linux/stat.h>
118#include <linux/time.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -0700119#include <u-boot/crc.h>
Stuart Wood471a2f02008-06-02 16:40:08 -0400120#include <watchdog.h>
wdenkfe8c2802002-11-03 00:38:21 +0000121#include <jffs2/jffs2.h>
122#include <jffs2/jffs2_1pass.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +0000123#include <linux/compat.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +0900124#include <linux/errno.h>
wdenkfe8c2802002-11-03 00:38:21 +0000125
126#include "jffs2_private.h"
127
Wolfgang Denka1be4762008-05-20 16:00:29 +0200128#define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
129#define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */
wdenkfe8c2802002-11-03 00:38:21 +0000130
wdenk6b58f332003-03-14 20:47:52 +0000131/* Debugging switches */
132#undef DEBUG_DIRENTS /* print directory entry list after scan */
133#undef DEBUG_FRAGMENTS /* print fragment list after scan */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200134#undef DEBUG /* enable debugging messages */
wdenkfe8c2802002-11-03 00:38:21 +0000135
wdenkfe8c2802002-11-03 00:38:21 +0000136#ifdef DEBUG
137# define DEBUGF(fmt,args...) printf(fmt ,##args)
138#else
139# define DEBUGF(fmt,args...)
140#endif
141
Ilya Yanoka933bd62008-11-13 19:49:35 +0300142#include "summary.h"
143
Wolfgang Denk47f57792005-08-08 01:03:24 +0200144/* keeps pointer to currentlu processed partition */
145static struct part_info *current_part;
wdenk8886a662004-04-18 19:43:36 +0000146
Wolfgang Denk15888b42007-07-05 17:56:27 +0200147#if (defined(CONFIG_JFFS2_NAND) && \
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500148 defined(CONFIG_CMD_NAND) )
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100149#include <nand.h>
wdenk8886a662004-04-18 19:43:36 +0000150/*
151 * Support for jffs2 on top of NAND-flash
152 *
153 * NAND memory isn't mapped in processor's address space,
154 * so data should be fetched from flash before
155 * being processed. This is exactly what functions declared
156 * here do.
157 *
158 */
159
wdenk8886a662004-04-18 19:43:36 +0000160#define NAND_PAGE_SIZE 512
161#define NAND_PAGE_SHIFT 9
162#define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1))
163
164#ifndef NAND_CACHE_PAGES
165#define NAND_CACHE_PAGES 16
166#endif
167#define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE)
168
169static u8* nand_cache = NULL;
170static u32 nand_cache_off = (u32)-1;
wdenk8886a662004-04-18 19:43:36 +0000171
172static int read_nand_cached(u32 off, u32 size, u_char *buf)
173{
Wolfgang Denk47f57792005-08-08 01:03:24 +0200174 struct mtdids *id = current_part->dev->id;
Grygorii Strashkoac8f8e32017-06-26 19:12:57 -0500175 struct mtd_info *mtd;
wdenk8886a662004-04-18 19:43:36 +0000176 u32 bytes_read = 0;
Marian Balakowicz6a076752006-04-08 19:08:06 +0200177 size_t retlen;
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300178 size_t toread;
wdenk8886a662004-04-18 19:43:36 +0000179 int cpy_bytes;
180
Grygorii Strashkoac8f8e32017-06-26 19:12:57 -0500181 mtd = get_nand_dev_by_index(id->num);
182 if (!mtd)
183 return -1;
184
wdenk8886a662004-04-18 19:43:36 +0000185 while (bytes_read < size) {
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300186 retlen = NAND_CACHE_SIZE;
187 if( nand_cache_off + retlen > mtd->size )
188 retlen = mtd->size - nand_cache_off;
189
wdenk8886a662004-04-18 19:43:36 +0000190 if ((off + bytes_read < nand_cache_off) ||
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300191 (off + bytes_read >= nand_cache_off + retlen)) {
wdenk8886a662004-04-18 19:43:36 +0000192 nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK;
193 if (!nand_cache) {
194 /* This memory never gets freed but 'cause
195 it's a bootloader, nobody cares */
wdenk12490652004-04-18 21:13:41 +0000196 nand_cache = malloc(NAND_CACHE_SIZE);
197 if (!nand_cache) {
wdenk8886a662004-04-18 19:43:36 +0000198 printf("read_nand_cached: can't alloc cache size %d bytes\n",
199 NAND_CACHE_SIZE);
200 return -1;
201 }
202 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100203
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300204 toread = NAND_CACHE_SIZE;
205 if( nand_cache_off + toread > mtd->size )
206 toread = mtd->size - nand_cache_off;
207
208 retlen = toread;
Grygorii Strashkoac8f8e32017-06-26 19:12:57 -0500209 if (nand_read(mtd, nand_cache_off,
Engling, Uwee5e23ee2017-10-10 14:20:55 +0000210 &retlen, nand_cache) < 0 ||
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300211 retlen != toread) {
wdenk8886a662004-04-18 19:43:36 +0000212 printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300213 nand_cache_off, toread);
wdenk8886a662004-04-18 19:43:36 +0000214 return -1;
215 }
216 }
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300217 cpy_bytes = nand_cache_off + retlen - (off + bytes_read);
wdenk8886a662004-04-18 19:43:36 +0000218 if (cpy_bytes > size - bytes_read)
219 cpy_bytes = size - bytes_read;
220 memcpy(buf + bytes_read,
221 nand_cache + off + bytes_read - nand_cache_off,
222 cpy_bytes);
223 bytes_read += cpy_bytes;
224 }
225 return bytes_read;
226}
227
Wolfgang Denk47f57792005-08-08 01:03:24 +0200228static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf)
wdenk8886a662004-04-18 19:43:36 +0000229{
230 u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
231
232 if (NULL == buf) {
Wolfgang Denk47f57792005-08-08 01:03:24 +0200233 printf("get_fl_mem_nand: can't alloc %d bytes\n", size);
wdenk8886a662004-04-18 19:43:36 +0000234 return NULL;
235 }
236 if (read_nand_cached(off, size, buf) < 0) {
wdenkf248ebc2004-05-05 19:44:41 +0000237 if (!ext_buf)
238 free(buf);
wdenk8886a662004-04-18 19:43:36 +0000239 return NULL;
240 }
241
242 return buf;
243}
244
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300245static void *get_node_mem_nand(u32 off, void *ext_buf)
wdenk8886a662004-04-18 19:43:36 +0000246{
247 struct jffs2_unknown_node node;
248 void *ret = NULL;
249
Wolfgang Denk47f57792005-08-08 01:03:24 +0200250 if (NULL == get_fl_mem_nand(off, sizeof(node), &node))
wdenk8886a662004-04-18 19:43:36 +0000251 return NULL;
252
Wolfgang Denk47f57792005-08-08 01:03:24 +0200253 if (!(ret = get_fl_mem_nand(off, node.magic ==
wdenk8886a662004-04-18 19:43:36 +0000254 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300255 ext_buf))) {
wdenk8886a662004-04-18 19:43:36 +0000256 printf("off = %#x magic %#x type %#x node.totlen = %d\n",
257 off, node.magic, node.nodetype, node.totlen);
258 }
259 return ret;
260}
261
Wolfgang Denk47f57792005-08-08 01:03:24 +0200262static void put_fl_mem_nand(void *buf)
wdenk8886a662004-04-18 19:43:36 +0000263{
264 free(buf);
265}
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500266#endif
Wolfgang Denk47f57792005-08-08 01:03:24 +0200267
Kyungmin Park25383742008-08-27 14:45:20 +0900268#if defined(CONFIG_CMD_ONENAND)
269
270#include <linux/mtd/mtd.h>
271#include <linux/mtd/onenand.h>
272#include <onenand_uboot.h>
273
274#define ONENAND_PAGE_SIZE 2048
275#define ONENAND_PAGE_SHIFT 11
276#define ONENAND_PAGE_MASK (~(ONENAND_PAGE_SIZE-1))
277
278#ifndef ONENAND_CACHE_PAGES
279#define ONENAND_CACHE_PAGES 4
280#endif
281#define ONENAND_CACHE_SIZE (ONENAND_CACHE_PAGES*ONENAND_PAGE_SIZE)
282
283static u8* onenand_cache;
284static u32 onenand_cache_off = (u32)-1;
285
286static int read_onenand_cached(u32 off, u32 size, u_char *buf)
287{
288 u32 bytes_read = 0;
289 size_t retlen;
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300290 size_t toread;
Kyungmin Park25383742008-08-27 14:45:20 +0900291 int cpy_bytes;
292
293 while (bytes_read < size) {
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300294 retlen = ONENAND_CACHE_SIZE;
295 if( onenand_cache_off + retlen > onenand_mtd.size )
296 retlen = onenand_mtd.size - onenand_cache_off;
297
Kyungmin Park25383742008-08-27 14:45:20 +0900298 if ((off + bytes_read < onenand_cache_off) ||
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300299 (off + bytes_read >= onenand_cache_off + retlen)) {
Kyungmin Park25383742008-08-27 14:45:20 +0900300 onenand_cache_off = (off + bytes_read) & ONENAND_PAGE_MASK;
301 if (!onenand_cache) {
302 /* This memory never gets freed but 'cause
303 it's a bootloader, nobody cares */
304 onenand_cache = malloc(ONENAND_CACHE_SIZE);
305 if (!onenand_cache) {
306 printf("read_onenand_cached: can't alloc cache size %d bytes\n",
307 ONENAND_CACHE_SIZE);
308 return -1;
309 }
310 }
311
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300312 toread = ONENAND_CACHE_SIZE;
313 if( onenand_cache_off + toread > onenand_mtd.size )
314 toread = onenand_mtd.size - onenand_cache_off;
315 retlen = toread;
Kyungmin Park25383742008-08-27 14:45:20 +0900316 if (onenand_read(&onenand_mtd, onenand_cache_off, retlen,
Engling, Uwee5e23ee2017-10-10 14:20:55 +0000317 &retlen, onenand_cache) < 0 ||
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300318 retlen != toread) {
Kyungmin Park25383742008-08-27 14:45:20 +0900319 printf("read_onenand_cached: error reading nand off %#x size %d bytes\n",
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300320 onenand_cache_off, toread);
Kyungmin Park25383742008-08-27 14:45:20 +0900321 return -1;
322 }
323 }
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300324 cpy_bytes = onenand_cache_off + retlen - (off + bytes_read);
Kyungmin Park25383742008-08-27 14:45:20 +0900325 if (cpy_bytes > size - bytes_read)
326 cpy_bytes = size - bytes_read;
327 memcpy(buf + bytes_read,
328 onenand_cache + off + bytes_read - onenand_cache_off,
329 cpy_bytes);
330 bytes_read += cpy_bytes;
331 }
332 return bytes_read;
333}
334
335static void *get_fl_mem_onenand(u32 off, u32 size, void *ext_buf)
336{
337 u_char *buf = ext_buf ? (u_char *)ext_buf : (u_char *)malloc(size);
338
339 if (NULL == buf) {
340 printf("get_fl_mem_onenand: can't alloc %d bytes\n", size);
341 return NULL;
342 }
343 if (read_onenand_cached(off, size, buf) < 0) {
344 if (!ext_buf)
345 free(buf);
346 return NULL;
347 }
348
349 return buf;
350}
351
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300352static void *get_node_mem_onenand(u32 off, void *ext_buf)
Kyungmin Park25383742008-08-27 14:45:20 +0900353{
354 struct jffs2_unknown_node node;
355 void *ret = NULL;
356
357 if (NULL == get_fl_mem_onenand(off, sizeof(node), &node))
358 return NULL;
359
360 ret = get_fl_mem_onenand(off, node.magic ==
361 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300362 ext_buf);
Kyungmin Park25383742008-08-27 14:45:20 +0900363 if (!ret) {
364 printf("off = %#x magic %#x type %#x node.totlen = %d\n",
365 off, node.magic, node.nodetype, node.totlen);
366 }
367 return ret;
368}
369
Kyungmin Park25383742008-08-27 14:45:20 +0900370static void put_fl_mem_onenand(void *buf)
371{
372 free(buf);
373}
374#endif
375
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500376#if defined(CONFIG_CMD_FLASH)
Tom Rini063c9382022-07-23 13:05:03 -0400377#include <flash.h>
378
Wolfgang Denk47f57792005-08-08 01:03:24 +0200379/*
380 * Support for jffs2 on top of NOR-flash
381 *
382 * NOR flash memory is mapped in processor's address space,
383 * just return address.
384 */
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300385static inline void *get_fl_mem_nor(u32 off, u32 size, void *ext_buf)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200386{
387 u32 addr = off;
388 struct mtdids *id = current_part->dev->id;
389
Wolfgang Denk47f57792005-08-08 01:03:24 +0200390 flash_info_t *flash = &flash_info[id->num];
391
392 addr += flash->start[0];
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300393 if (ext_buf) {
394 memcpy(ext_buf, (void *)addr, size);
395 return ext_buf;
396 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200397 return (void*)addr;
398}
399
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300400static inline void *get_node_mem_nor(u32 off, void *ext_buf)
Ilya Yanokddb02692008-11-13 19:49:33 +0300401{
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300402 struct jffs2_unknown_node *pNode;
Ilya Yanokddb02692008-11-13 19:49:33 +0300403
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300404 /* pNode will point directly to flash - don't provide external buffer
405 and don't care about size */
406 pNode = get_fl_mem_nor(off, 0, NULL);
407 return (void *)get_fl_mem_nor(off, pNode->magic == JFFS2_MAGIC_BITMASK ?
408 pNode->totlen : sizeof(*pNode), ext_buf);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200409}
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500410#endif
wdenk8886a662004-04-18 19:43:36 +0000411
Wolfgang Denk47f57792005-08-08 01:03:24 +0200412/*
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200413 * Generic jffs2 raw memory and node read routines.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200414 *
415 */
wdenk8886a662004-04-18 19:43:36 +0000416static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
417{
Wolfgang Denk47f57792005-08-08 01:03:24 +0200418 struct mtdids *id = current_part->dev->id;
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200419
Heiko Schocherd64a8132010-03-24 13:22:50 +0100420 switch(id->type) {
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500421#if defined(CONFIG_CMD_FLASH)
Heiko Schocherd64a8132010-03-24 13:22:50 +0100422 case MTD_DEV_TYPE_NOR:
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300423 return get_fl_mem_nor(off, size, ext_buf);
Heiko Schocherd64a8132010-03-24 13:22:50 +0100424 break;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200425#endif
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500426#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
Heiko Schocherd64a8132010-03-24 13:22:50 +0100427 case MTD_DEV_TYPE_NAND:
Wolfgang Denk47f57792005-08-08 01:03:24 +0200428 return get_fl_mem_nand(off, size, ext_buf);
Heiko Schocherd64a8132010-03-24 13:22:50 +0100429 break;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200430#endif
Kyungmin Park25383742008-08-27 14:45:20 +0900431#if defined(CONFIG_CMD_ONENAND)
Heiko Schocherd64a8132010-03-24 13:22:50 +0100432 case MTD_DEV_TYPE_ONENAND:
Kyungmin Park25383742008-08-27 14:45:20 +0900433 return get_fl_mem_onenand(off, size, ext_buf);
Heiko Schocherd64a8132010-03-24 13:22:50 +0100434 break;
Kyungmin Park25383742008-08-27 14:45:20 +0900435#endif
Heiko Schocherd64a8132010-03-24 13:22:50 +0100436 default:
437 printf("get_fl_mem: unknown device type, " \
438 "using raw offset!\n");
439 }
wdenk8886a662004-04-18 19:43:36 +0000440 return (void*)off;
441}
442
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300443static inline void *get_node_mem(u32 off, void *ext_buf)
wdenk8886a662004-04-18 19:43:36 +0000444{
Wolfgang Denk47f57792005-08-08 01:03:24 +0200445 struct mtdids *id = current_part->dev->id;
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200446
Heiko Schocherd64a8132010-03-24 13:22:50 +0100447 switch(id->type) {
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500448#if defined(CONFIG_CMD_FLASH)
Heiko Schocherd64a8132010-03-24 13:22:50 +0100449 case MTD_DEV_TYPE_NOR:
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300450 return get_node_mem_nor(off, ext_buf);
Heiko Schocherd64a8132010-03-24 13:22:50 +0100451 break;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200452#endif
Wolfgang Denk15888b42007-07-05 17:56:27 +0200453#if defined(CONFIG_JFFS2_NAND) && \
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500454 defined(CONFIG_CMD_NAND)
Heiko Schocherd64a8132010-03-24 13:22:50 +0100455 case MTD_DEV_TYPE_NAND:
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300456 return get_node_mem_nand(off, ext_buf);
Heiko Schocherd64a8132010-03-24 13:22:50 +0100457 break;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200458#endif
Kyungmin Park25383742008-08-27 14:45:20 +0900459#if defined(CONFIG_CMD_ONENAND)
Heiko Schocherd64a8132010-03-24 13:22:50 +0100460 case MTD_DEV_TYPE_ONENAND:
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300461 return get_node_mem_onenand(off, ext_buf);
Heiko Schocherd64a8132010-03-24 13:22:50 +0100462 break;
Kyungmin Park25383742008-08-27 14:45:20 +0900463#endif
Heiko Schocherd64a8132010-03-24 13:22:50 +0100464 default:
465 printf("get_fl_mem: unknown device type, " \
466 "using raw offset!\n");
467 }
wdenk8886a662004-04-18 19:43:36 +0000468 return (void*)off;
469}
470
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300471static inline void put_fl_mem(void *buf, void *ext_buf)
wdenk8886a662004-04-18 19:43:36 +0000472{
Wolfgang Denk47f57792005-08-08 01:03:24 +0200473 struct mtdids *id = current_part->dev->id;
wdenk8886a662004-04-18 19:43:36 +0000474
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300475 /* If buf is the same as ext_buf, it was provided by the caller -
476 we shouldn't free it then. */
477 if (buf == ext_buf)
478 return;
Scott Woodc97ba112008-10-31 13:51:12 -0500479 switch (id->type) {
480#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
481 case MTD_DEV_TYPE_NAND:
Wolfgang Denk47f57792005-08-08 01:03:24 +0200482 return put_fl_mem_nand(buf);
483#endif
Kyungmin Park25383742008-08-27 14:45:20 +0900484#if defined(CONFIG_CMD_ONENAND)
Scott Woodc97ba112008-10-31 13:51:12 -0500485 case MTD_DEV_TYPE_ONENAND:
Kyungmin Park25383742008-08-27 14:45:20 +0900486 return put_fl_mem_onenand(buf);
487#endif
Scott Woodc97ba112008-10-31 13:51:12 -0500488 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200489}
wdenk6b58f332003-03-14 20:47:52 +0000490
491/* Compression names */
492static char *compr_names[] = {
493 "NONE",
494 "ZERO",
495 "RTIME",
496 "RUBINMIPS",
497 "COPY",
498 "DYNRUBIN",
wdenke84ec902005-05-05 00:04:14 +0000499 "ZLIB",
Wolfgang Denk0eded4f2010-01-15 11:10:33 +0100500#if defined(CONFIG_JFFS2_LZO)
wdenke84ec902005-05-05 00:04:14 +0000501 "LZO",
wdenke84ec902005-05-05 00:04:14 +0000502#endif
wdenk6b58f332003-03-14 20:47:52 +0000503};
504
wdenk6b58f332003-03-14 20:47:52 +0000505/* Memory management */
506struct mem_block {
507 u32 index;
508 struct mem_block *next;
509 struct b_node nodes[NODE_CHUNK];
510};
511
wdenk6b58f332003-03-14 20:47:52 +0000512static void
513free_nodes(struct b_list *list)
514{
515 while (list->listMemBase != NULL) {
516 struct mem_block *next = list->listMemBase->next;
517 free( list->listMemBase );
518 list->listMemBase = next;
519 }
520}
wdenkfe8c2802002-11-03 00:38:21 +0000521
522static struct b_node *
wdenk6b58f332003-03-14 20:47:52 +0000523add_node(struct b_list *list)
wdenkfe8c2802002-11-03 00:38:21 +0000524{
wdenk6b58f332003-03-14 20:47:52 +0000525 u32 index = 0;
526 struct mem_block *memBase;
wdenkfe8c2802002-11-03 00:38:21 +0000527 struct b_node *b;
528
wdenk6b58f332003-03-14 20:47:52 +0000529 memBase = list->listMemBase;
530 if (memBase != NULL)
531 index = memBase->index;
wdenkfe8c2802002-11-03 00:38:21 +0000532#if 0
533 putLabeledWord("add_node: index = ", index);
wdenk6b58f332003-03-14 20:47:52 +0000534 putLabeledWord("add_node: memBase = ", list->listMemBase);
wdenkfe8c2802002-11-03 00:38:21 +0000535#endif
536
wdenk6b58f332003-03-14 20:47:52 +0000537 if (memBase == NULL || index >= NODE_CHUNK) {
538 /* we need more space before we continue */
539 memBase = mmalloc(sizeof(struct mem_block));
540 if (memBase == NULL) {
wdenkfe8c2802002-11-03 00:38:21 +0000541 putstr("add_node: malloc failed\n");
542 return NULL;
543 }
wdenk6b58f332003-03-14 20:47:52 +0000544 memBase->next = list->listMemBase;
545 index = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000546#if 0
547 putLabeledWord("add_node: alloced a new membase at ", *memBase);
548#endif
549
550 }
551 /* now we have room to add it. */
wdenk6b58f332003-03-14 20:47:52 +0000552 b = &memBase->nodes[index];
553 index ++;
wdenkfe8c2802002-11-03 00:38:21 +0000554
wdenk6b58f332003-03-14 20:47:52 +0000555 memBase->index = index;
556 list->listMemBase = memBase;
557 list->listCount++;
558 return b;
559}
wdenkfe8c2802002-11-03 00:38:21 +0000560
wdenk6b58f332003-03-14 20:47:52 +0000561static struct b_node *
Petr Borsodi80c25b12020-05-07 12:25:56 +0200562insert_node(struct b_list *list)
wdenk6b58f332003-03-14 20:47:52 +0000563{
564 struct b_node *new;
wdenkfe8c2802002-11-03 00:38:21 +0000565
wdenk6b58f332003-03-14 20:47:52 +0000566 if (!(new = add_node(list))) {
567 putstr("add_node failed!\r\n");
568 return NULL;
569 }
Mark Tomlinsonefdc2a62015-07-01 16:38:29 +1200570 new->next = NULL;
wdenk6b58f332003-03-14 20:47:52 +0000571
Mark Tomlinsonefdc2a62015-07-01 16:38:29 +1200572 if (list->listTail != NULL)
573 list->listTail->next = new;
wdenk6b58f332003-03-14 20:47:52 +0000574 else
Mark Tomlinsonefdc2a62015-07-01 16:38:29 +1200575 list->listHead = new;
576 list->listTail = new;
wdenkfe8c2802002-11-03 00:38:21 +0000577
wdenk6b58f332003-03-14 20:47:52 +0000578 return new;
wdenkfe8c2802002-11-03 00:38:21 +0000579}
580
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200581#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
wdenk2c9b05d2003-09-10 22:30:53 +0000582/* Sort data entries with the latest version last, so that if there
583 * is overlapping data the latest version will be used.
584 */
wdenk6b58f332003-03-14 20:47:52 +0000585static int compare_inodes(struct b_node *new, struct b_node *old)
586{
Petr Borsodi80c25b12020-05-07 12:25:56 +0200587 return new->version > old->version;
wdenk6b58f332003-03-14 20:47:52 +0000588}
589
wdenk2c9b05d2003-09-10 22:30:53 +0000590/* Sort directory entries so all entries in the same directory
591 * with the same name are grouped together, with the latest version
592 * last. This makes it easy to eliminate all but the latest version
593 * by marking the previous version dead by setting the inode to 0.
594 */
wdenk6b58f332003-03-14 20:47:52 +0000595static int compare_dirents(struct b_node *new, struct b_node *old)
596{
Mark Tomlinson7c6558e2015-07-01 16:38:23 +1200597 /*
598 * Using NULL as the buffer for NOR flash prevents the entire node
599 * being read. This makes most comparisons much quicker as only one
600 * or two entries from the node will be used most of the time.
wdenk2c9b05d2003-09-10 22:30:53 +0000601 */
Mark Tomlinson7c6558e2015-07-01 16:38:23 +1200602 struct jffs2_raw_dirent *jNew = get_node_mem(new->offset, NULL);
603 struct jffs2_raw_dirent *jOld = get_node_mem(old->offset, NULL);
604 int cmp;
605 int ret;
wdenk2c9b05d2003-09-10 22:30:53 +0000606
Mark Tomlinson7c6558e2015-07-01 16:38:23 +1200607 if (jNew->pino != jOld->pino) {
608 /* ascending sort by pino */
609 ret = jNew->pino > jOld->pino;
610 } else if (jNew->nsize != jOld->nsize) {
611 /*
612 * pino is the same, so use ascending sort by nsize,
613 * so we don't do strncmp unless we really must.
wdenk2c9b05d2003-09-10 22:30:53 +0000614 */
Mark Tomlinson7c6558e2015-07-01 16:38:23 +1200615 ret = jNew->nsize > jOld->nsize;
616 } else {
617 /*
618 * length is also the same, so use ascending sort by name
619 */
620 cmp = strncmp((char *)jNew->name, (char *)jOld->name,
621 jNew->nsize);
622 if (cmp != 0) {
623 ret = cmp > 0;
624 } else {
625 /*
626 * we have duplicate names in this directory,
627 * so use ascending sort by version
628 */
629 ret = jNew->version > jOld->version;
630 }
wdenk2c9b05d2003-09-10 22:30:53 +0000631 }
Mark Tomlinson7c6558e2015-07-01 16:38:23 +1200632 put_fl_mem(jNew, NULL);
633 put_fl_mem(jOld, NULL);
wdenk9c53f402003-10-15 23:53:47 +0000634
Mark Tomlinson7c6558e2015-07-01 16:38:23 +1200635 return ret;
wdenk6b58f332003-03-14 20:47:52 +0000636}
637#endif
wdenkfe8c2802002-11-03 00:38:21 +0000638
Wolfgang Denk47f57792005-08-08 01:03:24 +0200639void
640jffs2_free_cache(struct part_info *part)
wdenkfe8c2802002-11-03 00:38:21 +0000641{
wdenk6b58f332003-03-14 20:47:52 +0000642 struct b_lists *pL;
643
644 if (part->jffs2_priv != NULL) {
645 pL = (struct b_lists *)part->jffs2_priv;
646 free_nodes(&pL->frag);
647 free_nodes(&pL->dir);
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300648 free(pL->readbuf);
wdenk6b58f332003-03-14 20:47:52 +0000649 free(pL);
650 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200651}
652
653static u32
654jffs_init_1pass_list(struct part_info *part)
655{
656 struct b_lists *pL;
657
658 jffs2_free_cache(part);
659
wdenk6b58f332003-03-14 20:47:52 +0000660 if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
661 pL = (struct b_lists *)part->jffs2_priv;
wdenkfe8c2802002-11-03 00:38:21 +0000662
wdenk6b58f332003-03-14 20:47:52 +0000663 memset(pL, 0, sizeof(*pL));
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200664#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
wdenk6b58f332003-03-14 20:47:52 +0000665 pL->dir.listCompare = compare_dirents;
666 pL->frag.listCompare = compare_inodes;
667#endif
wdenkfe8c2802002-11-03 00:38:21 +0000668 }
669 return 0;
670}
671
672/* find the inode from the slashless name given a parent */
673static long
674jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
675{
676 struct b_node *b;
677 struct jffs2_raw_inode *jNode;
wdenk6b58f332003-03-14 20:47:52 +0000678 u32 totalSize = 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000679 u32 latestVersion = 0;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200680 uchar *lDest;
681 uchar *src;
wdenkfe8c2802002-11-03 00:38:21 +0000682 int i;
683 u32 counter = 0;
Petr Borsodi80c25b12020-05-07 12:25:56 +0200684
wdenk2c9b05d2003-09-10 22:30:53 +0000685 /* Find file size before loading any data, so fragments that
686 * start past the end of file can be ignored. A fragment
687 * that is partially in the file is loaded, so extra data may
688 * be loaded up to the next 4K boundary above the file size.
689 * This shouldn't cause trouble when loading kernel images, so
690 * we will live with it.
691 */
Petr Borsodi80c25b12020-05-07 12:25:56 +0200692 int latestOffset = -1;
wdenk2c9b05d2003-09-10 22:30:53 +0000693 for (b = pL->frag.listHead; b != NULL; b = b->next) {
Petr Borsodi80c25b12020-05-07 12:25:56 +0200694 if (inode == b->ino) {
wdenk2c9b05d2003-09-10 22:30:53 +0000695 /* get actual file length from the newest node */
Petr Borsodi80c25b12020-05-07 12:25:56 +0200696 if (b->version >= latestVersion) {
697 latestVersion = b->version;
698 latestOffset = b->offset;
wdenk2c9b05d2003-09-10 22:30:53 +0000699 }
700 }
Petr Borsodi80c25b12020-05-07 12:25:56 +0200701 }
702
703 if (latestOffset >= 0) {
704 jNode = (struct jffs2_raw_inode *)get_fl_mem(latestOffset,
705 sizeof(struct jffs2_raw_inode), pL->readbuf);
706 totalSize = jNode->isize;
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300707 put_fl_mem(jNode, pL->readbuf);
wdenk2c9b05d2003-09-10 22:30:53 +0000708 }
Petr Borsodi80c25b12020-05-07 12:25:56 +0200709
Mark Tomlinson6d998452015-07-01 16:38:22 +1200710 /*
711 * If no destination is provided, we are done.
712 * Just return the total size.
713 */
714 if (!dest)
715 return totalSize;
wdenkfe8c2802002-11-03 00:38:21 +0000716
wdenk6b58f332003-03-14 20:47:52 +0000717 for (b = pL->frag.listHead; b != NULL; b = b->next) {
Petr Borsodi80c25b12020-05-07 12:25:56 +0200718 if (inode == b->ino) {
719 /*
720 * Copy just the node and not the data at this point,
721 * since we don't yet know if we need this data.
722 */
723 jNode = (struct jffs2_raw_inode *)get_fl_mem(b->offset,
724 sizeof(struct jffs2_raw_inode),
725 pL->readbuf);
wdenk6b58f332003-03-14 20:47:52 +0000726#if 0
wdenkfe8c2802002-11-03 00:38:21 +0000727 putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
728 putLabeledWord("read_inode: inode = ", jNode->ino);
729 putLabeledWord("read_inode: version = ", jNode->version);
730 putLabeledWord("read_inode: isize = ", jNode->isize);
731 putLabeledWord("read_inode: offset = ", jNode->offset);
732 putLabeledWord("read_inode: csize = ", jNode->csize);
733 putLabeledWord("read_inode: dsize = ", jNode->dsize);
734 putLabeledWord("read_inode: compr = ", jNode->compr);
735 putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
736 putLabeledWord("read_inode: flags = ", jNode->flags);
wdenkfe8c2802002-11-03 00:38:21 +0000737#endif
wdenk2c9b05d2003-09-10 22:30:53 +0000738
wdenkfe8c2802002-11-03 00:38:21 +0000739 if(dest) {
Mark Tomlinsonb71748f2015-07-01 16:38:25 +1200740 /*
741 * Now that the inode has been checked,
742 * read the entire inode, including data.
743 */
744 put_fl_mem(jNode, pL->readbuf);
745 jNode = (struct jffs2_raw_inode *)
746 get_node_mem(b->offset, pL->readbuf);
747 src = ((uchar *)jNode) +
748 sizeof(struct jffs2_raw_inode);
wdenk6b58f332003-03-14 20:47:52 +0000749 /* ignore data behind latest known EOF */
wdenk8886a662004-04-18 19:43:36 +0000750 if (jNode->offset > totalSize) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300751 put_fl_mem(jNode, pL->readbuf);
wdenk6b58f332003-03-14 20:47:52 +0000752 continue;
wdenk8886a662004-04-18 19:43:36 +0000753 }
Ilya Yanokc0c07732008-11-13 19:49:36 +0300754 if (b->datacrc == CRC_UNKNOWN)
755 b->datacrc = data_crc(jNode) ?
756 CRC_OK : CRC_BAD;
757 if (b->datacrc == CRC_BAD) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300758 put_fl_mem(jNode, pL->readbuf);
Ilya Yanokddb02692008-11-13 19:49:33 +0300759 continue;
760 }
wdenk6b58f332003-03-14 20:47:52 +0000761
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200762 lDest = (uchar *) (dest + jNode->offset);
wdenkfe8c2802002-11-03 00:38:21 +0000763#if 0
wdenk6b58f332003-03-14 20:47:52 +0000764 putLabeledWord("read_inode: src = ", src);
wdenkfe8c2802002-11-03 00:38:21 +0000765 putLabeledWord("read_inode: dest = ", lDest);
wdenkfe8c2802002-11-03 00:38:21 +0000766#endif
767 switch (jNode->compr) {
768 case JFFS2_COMPR_NONE:
Wolfgang Denkbf913c12011-10-05 22:58:11 +0200769 ldr_memcpy(lDest, src, jNode->dsize);
wdenkfe8c2802002-11-03 00:38:21 +0000770 break;
771 case JFFS2_COMPR_ZERO:
wdenkfe8c2802002-11-03 00:38:21 +0000772 for (i = 0; i < jNode->dsize; i++)
773 *(lDest++) = 0;
774 break;
775 case JFFS2_COMPR_RTIME:
wdenkfe8c2802002-11-03 00:38:21 +0000776 rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
777 break;
778 case JFFS2_COMPR_DYNRUBIN:
779 /* this is slow but it works */
wdenkfe8c2802002-11-03 00:38:21 +0000780 dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
781 break;
782 case JFFS2_COMPR_ZLIB:
Wolfgang Denkbf913c12011-10-05 22:58:11 +0200783 zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
wdenkfe8c2802002-11-03 00:38:21 +0000784 break;
Wolfgang Denk0eded4f2010-01-15 11:10:33 +0100785#if defined(CONFIG_JFFS2_LZO)
wdenke84ec902005-05-05 00:04:14 +0000786 case JFFS2_COMPR_LZO:
Wolfgang Denkbf913c12011-10-05 22:58:11 +0200787 lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
wdenke84ec902005-05-05 00:04:14 +0000788 break;
789#endif
wdenkfe8c2802002-11-03 00:38:21 +0000790 default:
791 /* unknown */
Loïc Minier5d0569a2011-02-03 22:04:26 +0100792 putLabeledWord("UNKNOWN COMPRESSION METHOD = ", jNode->compr);
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300793 put_fl_mem(jNode, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +0000794 return -1;
795 break;
796 }
797 }
798
wdenkfe8c2802002-11-03 00:38:21 +0000799#if 0
wdenkfe8c2802002-11-03 00:38:21 +0000800 putLabeledWord("read_inode: totalSize = ", totalSize);
wdenkfe8c2802002-11-03 00:38:21 +0000801#endif
Petr Borsodi80c25b12020-05-07 12:25:56 +0200802 put_fl_mem(jNode, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +0000803 }
wdenkfe8c2802002-11-03 00:38:21 +0000804 counter++;
805 }
wdenkfe8c2802002-11-03 00:38:21 +0000806
807#if 0
wdenk6b58f332003-03-14 20:47:52 +0000808 putLabeledWord("read_inode: returning = ", totalSize);
wdenkfe8c2802002-11-03 00:38:21 +0000809#endif
wdenk6b58f332003-03-14 20:47:52 +0000810 return totalSize;
wdenkfe8c2802002-11-03 00:38:21 +0000811}
812
813/* find the inode from the slashless name given a parent */
814static u32
815jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
816{
817 struct b_node *b;
818 struct jffs2_raw_dirent *jDir;
819 int len;
820 u32 counter;
821 u32 version = 0;
822 u32 inode = 0;
823
824 /* name is assumed slash free */
825 len = strlen(name);
826
827 counter = 0;
828 /* we need to search all and return the inode with the highest version */
wdenk6b58f332003-03-14 20:47:52 +0000829 for(b = pL->dir.listHead; b; b = b->next, counter++) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300830 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
831 pL->readbuf);
wdenk6b58f332003-03-14 20:47:52 +0000832 if ((pino == jDir->pino) && (len == jDir->nsize) &&
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200833 (!strncmp((char *)jDir->name, name, len))) { /* a match */
wdenk8886a662004-04-18 19:43:36 +0000834 if (jDir->version < version) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300835 put_fl_mem(jDir, pL->readbuf);
wdenk2c9b05d2003-09-10 22:30:53 +0000836 continue;
wdenk8886a662004-04-18 19:43:36 +0000837 }
wdenkfe8c2802002-11-03 00:38:21 +0000838
wdenk2c9b05d2003-09-10 22:30:53 +0000839 if (jDir->version == version && inode != 0) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200840 /* I'm pretty sure this isn't legal */
wdenkfe8c2802002-11-03 00:38:21 +0000841 putstr(" ** ERROR ** ");
842 putnstr(jDir->name, jDir->nsize);
843 putLabeledWord(" has dup version =", version);
844 }
845 inode = jDir->ino;
846 version = jDir->version;
847 }
848#if 0
849 putstr("\r\nfind_inode:p&l ->");
850 putnstr(jDir->name, jDir->nsize);
851 putstr("\r\n");
852 putLabeledWord("pino = ", jDir->pino);
853 putLabeledWord("nsize = ", jDir->nsize);
854 putLabeledWord("b = ", (u32) b);
855 putLabeledWord("counter = ", counter);
856#endif
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300857 put_fl_mem(jDir, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +0000858 }
859 return inode;
860}
861
wdenkad276f22004-01-04 16:28:35 +0000862char *mkmodestr(unsigned long mode, char *str)
wdenkfe8c2802002-11-03 00:38:21 +0000863{
wdenk6b58f332003-03-14 20:47:52 +0000864 static const char *l = "xwr";
865 int mask = 1, i;
866 char c;
wdenkfe8c2802002-11-03 00:38:21 +0000867
wdenk6b58f332003-03-14 20:47:52 +0000868 switch (mode & S_IFMT) {
869 case S_IFDIR: str[0] = 'd'; break;
870 case S_IFBLK: str[0] = 'b'; break;
871 case S_IFCHR: str[0] = 'c'; break;
872 case S_IFIFO: str[0] = 'f'; break;
873 case S_IFLNK: str[0] = 'l'; break;
874 case S_IFSOCK: str[0] = 's'; break;
875 case S_IFREG: str[0] = '-'; break;
876 default: str[0] = '?';
877 }
wdenkfe8c2802002-11-03 00:38:21 +0000878
wdenk6b58f332003-03-14 20:47:52 +0000879 for(i = 0; i < 9; i++) {
880 c = l[i%3];
881 str[9-i] = (mode & mask)?c:'-';
882 mask = mask<<1;
883 }
wdenkfe8c2802002-11-03 00:38:21 +0000884
wdenk6b58f332003-03-14 20:47:52 +0000885 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
886 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
887 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
888 str[10] = '\0';
889 return str;
wdenkfe8c2802002-11-03 00:38:21 +0000890}
891
892static inline void dump_stat(struct stat *st, const char *name)
893{
wdenk6b58f332003-03-14 20:47:52 +0000894 char str[20];
895 char s[64], *p;
wdenkfe8c2802002-11-03 00:38:21 +0000896
wdenk6b58f332003-03-14 20:47:52 +0000897 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
898 st->st_mtime = 1;
wdenkfe8c2802002-11-03 00:38:21 +0000899
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200900 ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
wdenkfe8c2802002-11-03 00:38:21 +0000901
wdenk6b58f332003-03-14 20:47:52 +0000902 if ((p = strchr(s,'\n')) != NULL) *p = '\0';
903 if ((p = strchr(s,'\r')) != NULL) *p = '\0';
wdenkfe8c2802002-11-03 00:38:21 +0000904
905/*
wdenk6b58f332003-03-14 20:47:52 +0000906 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
907 st->st_size, s, name);
wdenkfe8c2802002-11-03 00:38:21 +0000908*/
909
wdenk6b58f332003-03-14 20:47:52 +0000910 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
wdenkfe8c2802002-11-03 00:38:21 +0000911}
912
913static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
914{
915 char fname[256];
916 struct stat st;
917
918 if(!d || !i) return -1;
919
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200920 strncpy(fname, (char *)d->name, d->nsize);
wdenk6b58f332003-03-14 20:47:52 +0000921 fname[d->nsize] = '\0';
wdenkfe8c2802002-11-03 00:38:21 +0000922
923 memset(&st,0,sizeof(st));
924
wdenk6b58f332003-03-14 20:47:52 +0000925 st.st_mtime = i->mtime;
926 st.st_mode = i->mode;
927 st.st_ino = i->ino;
Ilya Yanokc4785fb2008-11-13 19:49:31 +0300928 st.st_size = i->isize;
wdenkfe8c2802002-11-03 00:38:21 +0000929
930 dump_stat(&st, fname);
931
932 if (d->type == DT_LNK) {
933 unsigned char *src = (unsigned char *) (&i[1]);
934 putstr(" -> ");
935 putnstr(src, (int)i->dsize);
936 }
937
938 putstr("\r\n");
939
940 return 0;
941}
942
943/* list inodes with the given pino */
944static u32
945jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
946{
947 struct b_node *b;
948 struct jffs2_raw_dirent *jDir;
949
wdenk6b58f332003-03-14 20:47:52 +0000950 for (b = pL->dir.listHead; b; b = b->next) {
Petr Borsodi80c25b12020-05-07 12:25:56 +0200951 if (pino == b->pino) {
wdenk6b58f332003-03-14 20:47:52 +0000952 u32 i_version = 0;
Petr Borsodi80c25b12020-05-07 12:25:56 +0200953 int i_offset = -1;
954 struct jffs2_raw_inode *jNode = NULL;
Mark Tomlinson29468522015-07-01 16:38:24 +1200955 struct b_node *b2;
wdenkfe8c2802002-11-03 00:38:21 +0000956
Petr Borsodi80c25b12020-05-07 12:25:56 +0200957 jDir = (struct jffs2_raw_dirent *)
958 get_node_mem(b->offset, pL->readbuf);
Mark Tomlinson29468522015-07-01 16:38:24 +1200959#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
960 /* Check for more recent versions of this file */
961 int match;
962 do {
963 struct b_node *next = b->next;
964 struct jffs2_raw_dirent *jDirNext;
965 if (!next)
966 break;
967 jDirNext = (struct jffs2_raw_dirent *)
968 get_node_mem(next->offset, NULL);
969 match = jDirNext->pino == jDir->pino &&
970 jDirNext->nsize == jDir->nsize &&
971 strncmp((char *)jDirNext->name,
972 (char *)jDir->name,
973 jDir->nsize) == 0;
974 if (match) {
975 /* Use next. It is more recent */
976 b = next;
977 /* Update buffer with the new info */
978 *jDir = *jDirNext;
979 }
980 put_fl_mem(jDirNext, NULL);
981 } while (match);
982#endif
983 if (jDir->ino == 0) {
984 /* Deleted file */
985 put_fl_mem(jDir, pL->readbuf);
986 continue;
987 }
988
989 for (b2 = pL->frag.listHead; b2; b2 = b2->next) {
Petr Borsodi80c25b12020-05-07 12:25:56 +0200990 if (b2->ino == jDir->ino &&
991 b2->version >= i_version) {
992 i_version = b2->version;
993 i_offset = b2->offset;
wdenkf248ebc2004-05-05 19:44:41 +0000994 }
wdenkfe8c2802002-11-03 00:38:21 +0000995 }
996
Petr Borsodi80c25b12020-05-07 12:25:56 +0200997 if (i_version >= 0) {
998 if (jDir->type == DT_LNK)
999 jNode = get_node_mem(i_offset, NULL);
1000 else
1001 jNode = get_fl_mem(i_offset,
1002 sizeof(*jNode),
1003 NULL);
1004 }
1005
1006 dump_inode(pL, jDir, jNode);
1007 put_fl_mem(jNode, NULL);
1008
1009 put_fl_mem(jDir, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001010 }
1011 }
1012 return pino;
1013}
1014
1015static u32
1016jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
1017{
1018 int i;
1019 char tmp[256];
1020 char working_tmp[256];
1021 char *c;
1022
1023 /* discard any leading slash */
1024 i = 0;
1025 while (fname[i] == '/')
1026 i++;
1027 strcpy(tmp, &fname[i]);
1028
1029 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
1030 {
1031 strncpy(working_tmp, tmp, c - tmp);
1032 working_tmp[c - tmp] = '\0';
1033#if 0
1034 putstr("search_inode: tmp = ");
1035 putstr(tmp);
1036 putstr("\r\n");
1037 putstr("search_inode: wtmp = ");
1038 putstr(working_tmp);
1039 putstr("\r\n");
1040 putstr("search_inode: c = ");
1041 putstr(c);
1042 putstr("\r\n");
1043#endif
1044 for (i = 0; i < strlen(c) - 1; i++)
1045 tmp[i] = c[i + 1];
1046 tmp[i] = '\0';
1047#if 0
1048 putstr("search_inode: post tmp = ");
1049 putstr(tmp);
1050 putstr("\r\n");
1051#endif
1052
1053 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
1054 putstr("find_inode failed for name=");
1055 putstr(working_tmp);
1056 putstr("\r\n");
1057 return 0;
1058 }
1059 }
1060 /* this is for the bare filename, directories have already been mapped */
1061 if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1062 putstr("find_inode failed for name=");
1063 putstr(tmp);
1064 putstr("\r\n");
1065 return 0;
1066 }
1067 return pino;
1068
1069}
1070
1071static u32
1072jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
1073{
1074 struct b_node *b;
1075 struct b_node *b2;
1076 struct jffs2_raw_dirent *jDir;
1077 struct jffs2_raw_inode *jNode;
wdenk8886a662004-04-18 19:43:36 +00001078 u8 jDirFoundType = 0;
1079 u32 jDirFoundIno = 0;
1080 u32 jDirFoundPino = 0;
wdenkfe8c2802002-11-03 00:38:21 +00001081 char tmp[256];
1082 u32 version = 0;
1083 u32 pino;
1084 unsigned char *src;
1085
1086 /* we need to search all and return the inode with the highest version */
wdenk6b58f332003-03-14 20:47:52 +00001087 for(b = pL->dir.listHead; b; b = b->next) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001088 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
1089 pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001090 if (ino == jDir->ino) {
Wolfgang Denka1be4762008-05-20 16:00:29 +02001091 if (jDir->version < version) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001092 put_fl_mem(jDir, pL->readbuf);
wdenk2c9b05d2003-09-10 22:30:53 +00001093 continue;
wdenk8886a662004-04-18 19:43:36 +00001094 }
wdenkfe8c2802002-11-03 00:38:21 +00001095
wdenk8886a662004-04-18 19:43:36 +00001096 if (jDir->version == version && jDirFoundType) {
Wolfgang Denka1be4762008-05-20 16:00:29 +02001097 /* I'm pretty sure this isn't legal */
wdenkfe8c2802002-11-03 00:38:21 +00001098 putstr(" ** ERROR ** ");
1099 putnstr(jDir->name, jDir->nsize);
1100 putLabeledWord(" has dup version (resolve) = ",
1101 version);
1102 }
1103
wdenk8886a662004-04-18 19:43:36 +00001104 jDirFoundType = jDir->type;
1105 jDirFoundIno = jDir->ino;
1106 jDirFoundPino = jDir->pino;
wdenkfe8c2802002-11-03 00:38:21 +00001107 version = jDir->version;
1108 }
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001109 put_fl_mem(jDir, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001110 }
1111 /* now we found the right entry again. (shoulda returned inode*) */
wdenk8886a662004-04-18 19:43:36 +00001112 if (jDirFoundType != DT_LNK)
1113 return jDirFoundIno;
wdenk6b58f332003-03-14 20:47:52 +00001114
1115 /* it's a soft link so we follow it again. */
1116 b2 = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +00001117 while (b2) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001118 jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset,
1119 pL->readbuf);
wdenk8886a662004-04-18 19:43:36 +00001120 if (jNode->ino == jDirFoundIno) {
wdenk6c59edc2004-05-03 20:45:30 +00001121 src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
wdenkfe8c2802002-11-03 00:38:21 +00001122
1123#if 0
1124 putLabeledWord("\t\t dsize = ", jNode->dsize);
1125 putstr("\t\t target = ");
1126 putnstr(src, jNode->dsize);
1127 putstr("\r\n");
1128#endif
Wolfgang Denk7fb52662005-10-13 16:45:02 +02001129 strncpy(tmp, (char *)src, jNode->dsize);
wdenkfe8c2802002-11-03 00:38:21 +00001130 tmp[jNode->dsize] = '\0';
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001131 put_fl_mem(jNode, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001132 break;
1133 }
1134 b2 = b2->next;
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001135 put_fl_mem(jNode, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001136 }
1137 /* ok so the name of the new file to find is in tmp */
1138 /* if it starts with a slash it is root based else shared dirs */
1139 if (tmp[0] == '/')
1140 pino = 1;
1141 else
wdenk8886a662004-04-18 19:43:36 +00001142 pino = jDirFoundPino;
wdenkfe8c2802002-11-03 00:38:21 +00001143
1144 return jffs2_1pass_search_inode(pL, tmp, pino);
1145}
1146
1147static u32
1148jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
1149{
1150 int i;
1151 char tmp[256];
1152 char working_tmp[256];
1153 char *c;
1154
1155 /* discard any leading slash */
1156 i = 0;
1157 while (fname[i] == '/')
1158 i++;
1159 strcpy(tmp, &fname[i]);
1160 working_tmp[0] = '\0';
1161 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
1162 {
1163 strncpy(working_tmp, tmp, c - tmp);
1164 working_tmp[c - tmp] = '\0';
1165 for (i = 0; i < strlen(c) - 1; i++)
1166 tmp[i] = c[i + 1];
1167 tmp[i] = '\0';
1168 /* only a failure if we arent looking at top level */
wdenk6b58f332003-03-14 20:47:52 +00001169 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
1170 (working_tmp[0])) {
wdenkfe8c2802002-11-03 00:38:21 +00001171 putstr("find_inode failed for name=");
1172 putstr(working_tmp);
1173 putstr("\r\n");
1174 return 0;
1175 }
1176 }
1177
1178 if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1179 putstr("find_inode failed for name=");
1180 putstr(tmp);
1181 putstr("\r\n");
1182 return 0;
1183 }
1184 /* this is for the bare filename, directories have already been mapped */
1185 if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
1186 putstr("find_inode failed for name=");
1187 putstr(tmp);
1188 putstr("\r\n");
1189 return 0;
1190 }
1191 return pino;
1192
1193}
1194
1195unsigned char
1196jffs2_1pass_rescan_needed(struct part_info *part)
1197{
1198 struct b_node *b;
wdenk8886a662004-04-18 19:43:36 +00001199 struct jffs2_unknown_node onode;
wdenkfe8c2802002-11-03 00:38:21 +00001200 struct jffs2_unknown_node *node;
wdenk6b58f332003-03-14 20:47:52 +00001201 struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
wdenkfe8c2802002-11-03 00:38:21 +00001202
1203 if (part->jffs2_priv == 0){
1204 DEBUGF ("rescan: First time in use\n");
1205 return 1;
1206 }
Wolfgang Denk47f57792005-08-08 01:03:24 +02001207
wdenkfe8c2802002-11-03 00:38:21 +00001208 /* if we have no list, we need to rescan */
wdenk6b58f332003-03-14 20:47:52 +00001209 if (pL->frag.listCount == 0) {
wdenkfe8c2802002-11-03 00:38:21 +00001210 DEBUGF ("rescan: fraglist zero\n");
1211 return 1;
1212 }
1213
wdenk6b58f332003-03-14 20:47:52 +00001214 /* but suppose someone reflashed a partition at the same offset... */
1215 b = pL->dir.listHead;
wdenkfe8c2802002-11-03 00:38:21 +00001216 while (b) {
wdenk8886a662004-04-18 19:43:36 +00001217 node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
1218 sizeof(onode), &onode);
wdenkfe8c2802002-11-03 00:38:21 +00001219 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
wdenk6b58f332003-03-14 20:47:52 +00001220 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
1221 (unsigned long) b->offset);
wdenkfe8c2802002-11-03 00:38:21 +00001222 return 1;
1223 }
1224 b = b->next;
1225 }
1226 return 0;
1227}
wdenk6b58f332003-03-14 20:47:52 +00001228
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001229#ifdef CONFIG_JFFS2_SUMMARY
1230static u32 sum_get_unaligned32(u32 *ptr)
1231{
1232 u32 val;
1233 u8 *p = (u8 *)ptr;
1234
1235 val = *p | (*(p + 1) << 8) | (*(p + 2) << 16) | (*(p + 3) << 24);
1236
1237 return __le32_to_cpu(val);
1238}
1239
1240static u16 sum_get_unaligned16(u16 *ptr)
1241{
1242 u16 val;
1243 u8 *p = (u8 *)ptr;
1244
1245 val = *p | (*(p + 1) << 8);
1246
1247 return __le16_to_cpu(val);
1248}
1249
Ilya Yanoka933bd62008-11-13 19:49:35 +03001250#define dbg_summary(...) do {} while (0);
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001251/*
1252 * Process the stored summary information - helper function for
Ilya Yanoka933bd62008-11-13 19:49:35 +03001253 * jffs2_sum_scan_sumnode()
1254 */
1255
1256static int jffs2_sum_process_sum_data(struct part_info *part, uint32_t offset,
1257 struct jffs2_raw_summary *summary,
1258 struct b_lists *pL)
1259{
1260 void *sp;
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001261 int i, pass;
Petr Borsodi80c25b12020-05-07 12:25:56 +02001262 struct b_node *b;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001263
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001264 for (pass = 0; pass < 2; pass++) {
1265 sp = summary->sum;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001266
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001267 for (i = 0; i < summary->sum_num; i++) {
1268 struct jffs2_sum_unknown_flash *spu = sp;
1269 dbg_summary("processing summary index %d\n", i);
Ilya Yanoka933bd62008-11-13 19:49:35 +03001270
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001271 switch (sum_get_unaligned16(&spu->nodetype)) {
1272 case JFFS2_NODETYPE_INODE: {
Ilya Yanoka933bd62008-11-13 19:49:35 +03001273 struct jffs2_sum_inode_flash *spi;
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001274 if (pass) {
1275 spi = sp;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001276
Petr Borsodi80c25b12020-05-07 12:25:56 +02001277 b = insert_node(&pL->frag);
1278 if (!b)
1279 return -1;
1280 b->offset = (u32)part->offset +
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001281 offset +
1282 sum_get_unaligned32(
Petr Borsodi80c25b12020-05-07 12:25:56 +02001283 &spi->offset);
1284 b->version = sum_get_unaligned32(
1285 &spi->version);
1286 b->ino = sum_get_unaligned32(
1287 &spi->inode);
Wagner Popov dos Santosb9e3b672021-02-23 00:49:00 -03001288 b->datacrc = CRC_UNKNOWN;
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001289 }
Ilya Yanoka933bd62008-11-13 19:49:35 +03001290
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001291 sp += JFFS2_SUMMARY_INODE_SIZE;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001292
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001293 break;
1294 }
1295 case JFFS2_NODETYPE_DIRENT: {
1296 struct jffs2_sum_dirent_flash *spd;
1297 spd = sp;
1298 if (pass) {
Petr Borsodi80c25b12020-05-07 12:25:56 +02001299 b = insert_node(&pL->dir);
1300 if (!b)
1301 return -1;
1302 b->offset = (u32)part->offset +
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001303 offset +
1304 sum_get_unaligned32(
Petr Borsodi80c25b12020-05-07 12:25:56 +02001305 &spd->offset);
1306 b->version = sum_get_unaligned32(
1307 &spd->version);
1308 b->pino = sum_get_unaligned32(
1309 &spd->pino);
Wagner Popov dos Santosb9e3b672021-02-23 00:49:00 -03001310 b->datacrc = CRC_UNKNOWN;
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001311 }
Ilya Yanoka933bd62008-11-13 19:49:35 +03001312
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001313 sp += JFFS2_SUMMARY_DIRENT_SIZE(
1314 spd->nsize);
Ilya Yanoka933bd62008-11-13 19:49:35 +03001315
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001316 break;
1317 }
1318 default : {
1319 uint16_t nodetype = sum_get_unaligned16(
1320 &spu->nodetype);
1321 printf("Unsupported node type %x found"
1322 " in summary!\n",
1323 nodetype);
1324 if ((nodetype & JFFS2_COMPAT_MASK) ==
1325 JFFS2_FEATURE_INCOMPAT)
1326 return -EIO;
1327 return -EBADMSG;
1328 }
Ilya Yanoka933bd62008-11-13 19:49:35 +03001329 }
1330 }
1331 }
1332 return 0;
1333}
1334
1335/* Process the summary node - called from jffs2_scan_eraseblock() */
1336int jffs2_sum_scan_sumnode(struct part_info *part, uint32_t offset,
1337 struct jffs2_raw_summary *summary, uint32_t sumsize,
1338 struct b_lists *pL)
1339{
1340 struct jffs2_unknown_node crcnode;
Tom Rini7c4734d2016-03-27 14:48:36 -04001341 int ret, __maybe_unused ofs;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001342 uint32_t crc;
1343
1344 ofs = part->sector_size - sumsize;
1345
1346 dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
1347 offset, offset + ofs, sumsize);
1348
1349 /* OK, now check for node validity and CRC */
1350 crcnode.magic = JFFS2_MAGIC_BITMASK;
1351 crcnode.nodetype = JFFS2_NODETYPE_SUMMARY;
1352 crcnode.totlen = summary->totlen;
1353 crc = crc32_no_comp(0, (uchar *)&crcnode, sizeof(crcnode)-4);
1354
1355 if (summary->hdr_crc != crc) {
1356 dbg_summary("Summary node header is corrupt (bad CRC or "
1357 "no summary at all)\n");
1358 goto crc_err;
1359 }
1360
1361 if (summary->totlen != sumsize) {
1362 dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
1363 goto crc_err;
1364 }
1365
1366 crc = crc32_no_comp(0, (uchar *)summary,
1367 sizeof(struct jffs2_raw_summary)-8);
1368
1369 if (summary->node_crc != crc) {
1370 dbg_summary("Summary node is corrupt (bad CRC)\n");
1371 goto crc_err;
1372 }
1373
1374 crc = crc32_no_comp(0, (uchar *)summary->sum,
1375 sumsize - sizeof(struct jffs2_raw_summary));
1376
1377 if (summary->sum_crc != crc) {
1378 dbg_summary("Summary node data is corrupt (bad CRC)\n");
1379 goto crc_err;
1380 }
1381
1382 if (summary->cln_mkr)
1383 dbg_summary("Summary : CLEANMARKER node \n");
1384
1385 ret = jffs2_sum_process_sum_data(part, offset, summary, pL);
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001386 if (ret == -EBADMSG)
1387 return 0;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001388 if (ret)
1389 return ret; /* real error */
1390
1391 return 1;
1392
1393crc_err:
1394 putstr("Summary node crc error, skipping summary information.\n");
1395
1396 return 0;
1397}
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001398#endif /* CONFIG_JFFS2_SUMMARY */
Ilya Yanoka933bd62008-11-13 19:49:35 +03001399
wdenk6b58f332003-03-14 20:47:52 +00001400#ifdef DEBUG_FRAGMENTS
1401static void
1402dump_fragments(struct b_lists *pL)
1403{
1404 struct b_node *b;
wdenk8886a662004-04-18 19:43:36 +00001405 struct jffs2_raw_inode ojNode;
wdenk6b58f332003-03-14 20:47:52 +00001406 struct jffs2_raw_inode *jNode;
1407
1408 putstr("\r\n\r\n******The fragment Entries******\r\n");
1409 b = pL->frag.listHead;
1410 while (b) {
wdenk8886a662004-04-18 19:43:36 +00001411 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1412 sizeof(ojNode), &ojNode);
wdenk6b58f332003-03-14 20:47:52 +00001413 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
1414 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
1415 putLabeledWord("\tbuild_list: inode = ", jNode->ino);
1416 putLabeledWord("\tbuild_list: version = ", jNode->version);
1417 putLabeledWord("\tbuild_list: isize = ", jNode->isize);
1418 putLabeledWord("\tbuild_list: atime = ", jNode->atime);
1419 putLabeledWord("\tbuild_list: offset = ", jNode->offset);
1420 putLabeledWord("\tbuild_list: csize = ", jNode->csize);
1421 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
1422 putLabeledWord("\tbuild_list: compr = ", jNode->compr);
1423 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
1424 putLabeledWord("\tbuild_list: flags = ", jNode->flags);
wdenk57b2d802003-06-27 21:31:46 +00001425 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
wdenk6b58f332003-03-14 20:47:52 +00001426 b = b->next;
1427 }
1428}
1429#endif
1430
1431#ifdef DEBUG_DIRENTS
1432static void
1433dump_dirents(struct b_lists *pL)
1434{
1435 struct b_node *b;
1436 struct jffs2_raw_dirent *jDir;
1437
1438 putstr("\r\n\r\n******The directory Entries******\r\n");
1439 b = pL->dir.listHead;
1440 while (b) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001441 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
1442 pL->readbuf);
wdenk6b58f332003-03-14 20:47:52 +00001443 putstr("\r\n");
1444 putnstr(jDir->name, jDir->nsize);
1445 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
1446 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
1447 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
1448 putLabeledWord("\tbuild_list: pino = ", jDir->pino);
1449 putLabeledWord("\tbuild_list: version = ", jDir->version);
1450 putLabeledWord("\tbuild_list: ino = ", jDir->ino);
1451 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
1452 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
1453 putLabeledWord("\tbuild_list: type = ", jDir->type);
1454 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
1455 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
Wolfgang Denka1be4762008-05-20 16:00:29 +02001456 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
wdenk6b58f332003-03-14 20:47:52 +00001457 b = b->next;
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001458 put_fl_mem(jDir, pL->readbuf);
wdenk6b58f332003-03-14 20:47:52 +00001459 }
1460}
1461#endif
wdenkfe8c2802002-11-03 00:38:21 +00001462
Mark Tomlinsonf89bfd02015-07-01 16:38:27 +12001463#define DEFAULT_EMPTY_SCAN_SIZE 256
Ilya Yanokddb02692008-11-13 19:49:33 +03001464
1465static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size)
1466{
1467 if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
1468 return sector_size;
1469 else
1470 return DEFAULT_EMPTY_SCAN_SIZE;
1471}
1472
wdenkfe8c2802002-11-03 00:38:21 +00001473static u32
1474jffs2_1pass_build_lists(struct part_info * part)
1475{
1476 struct b_lists *pL;
Petr Borsodia61d1d12020-05-07 12:25:55 +02001477 union jffs2_node_union *node;
Tom Rini3cd84d62013-12-05 14:48:38 -05001478 u32 nr_sectors;
Ilya Yanokddb02692008-11-13 19:49:33 +03001479 u32 i;
wdenkfe8c2802002-11-03 00:38:21 +00001480 u32 counter4 = 0;
1481 u32 counterF = 0;
1482 u32 counterN = 0;
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001483 u32 max_totlen = 0;
Mark Tomlinsonef619822015-07-01 16:38:26 +12001484 u32 buf_size;
Ilya Yanokddb02692008-11-13 19:49:33 +03001485 char *buf;
wdenkfe8c2802002-11-03 00:38:21 +00001486
Tom Rini3cd84d62013-12-05 14:48:38 -05001487 nr_sectors = lldiv(part->size, part->sector_size);
wdenkfe8c2802002-11-03 00:38:21 +00001488 /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
1489 /* jffs2 list building enterprise nope. in newer versions the overhead is */
1490 /* only about 5 %. not enough to inconvenience people for. */
1491 /* lcd_off(); */
1492
1493 /* if we are building a list we need to refresh the cache. */
wdenkfe8c2802002-11-03 00:38:21 +00001494 jffs_init_1pass_list(part);
wdenk6b58f332003-03-14 20:47:52 +00001495 pL = (struct b_lists *)part->jffs2_priv;
Mark Tomlinsonef619822015-07-01 16:38:26 +12001496 buf = malloc(DEFAULT_EMPTY_SCAN_SIZE);
wdenk42c05472004-03-23 22:14:11 +00001497 puts ("Scanning JFFS2 FS: ");
wdenkfe8c2802002-11-03 00:38:21 +00001498
1499 /* start at the beginning of the partition */
Ilya Yanokddb02692008-11-13 19:49:33 +03001500 for (i = 0; i < nr_sectors; i++) {
1501 uint32_t sector_ofs = i * part->sector_size;
1502 uint32_t buf_ofs = sector_ofs;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001503 uint32_t buf_len;
Ilya Yanokddb02692008-11-13 19:49:33 +03001504 uint32_t ofs, prevofs;
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001505#ifdef CONFIG_JFFS2_SUMMARY
Ilya Yanoka933bd62008-11-13 19:49:35 +03001506 struct jffs2_sum_marker *sm;
1507 void *sumptr = NULL;
1508 uint32_t sumlen;
1509 int ret;
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001510#endif
Mark Tomlinson56a7f482015-07-01 16:38:28 +12001511 /* Indicates a sector with a CLEANMARKER was found */
1512 int clean_sector = 0;
Petr Borsodia61d1d12020-05-07 12:25:55 +02001513 struct jffs2_unknown_node crcnode;
Petr Borsodi80c25b12020-05-07 12:25:56 +02001514 struct b_node *b;
wdenk7ad5e4c2004-04-25 15:41:35 +00001515
Mark Tomlinsonef619822015-07-01 16:38:26 +12001516 /* Set buf_size to maximum length */
1517 buf_size = DEFAULT_EMPTY_SCAN_SIZE;
Stefan Roese80877fa2022-09-02 14:10:46 +02001518 schedule();
Ilya Yanoka933bd62008-11-13 19:49:35 +03001519
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001520#ifdef CONFIG_JFFS2_SUMMARY
Ilya Yanoka933bd62008-11-13 19:49:35 +03001521 buf_len = sizeof(*sm);
1522
1523 /* Read as much as we want into the _end_ of the preallocated
1524 * buffer
1525 */
1526 get_fl_mem(part->offset + sector_ofs + part->sector_size -
1527 buf_len, buf_len, buf + buf_size - buf_len);
1528
1529 sm = (void *)buf + buf_size - sizeof(*sm);
1530 if (sm->magic == JFFS2_SUM_MAGIC) {
1531 sumlen = part->sector_size - sm->offset;
1532 sumptr = buf + buf_size - sumlen;
1533
1534 /* Now, make sure the summary itself is available */
1535 if (sumlen > buf_size) {
1536 /* Need to kmalloc for this. */
1537 sumptr = malloc(sumlen);
1538 if (!sumptr) {
1539 putstr("Can't get memory for summary "
1540 "node!\n");
Ilya Yanoka87011e2009-08-12 16:42:48 +04001541 free(buf);
1542 jffs2_free_cache(part);
Ilya Yanoka933bd62008-11-13 19:49:35 +03001543 return 0;
1544 }
1545 memcpy(sumptr + sumlen - buf_len, buf +
1546 buf_size - buf_len, buf_len);
1547 }
1548 if (buf_len < sumlen) {
1549 /* Need to read more so that the entire summary
1550 * node is present
1551 */
1552 get_fl_mem(part->offset + sector_ofs +
1553 part->sector_size - sumlen,
1554 sumlen - buf_len, sumptr);
1555 }
1556 }
1557
1558 if (sumptr) {
1559 ret = jffs2_sum_scan_sumnode(part, sector_ofs, sumptr,
1560 sumlen, pL);
1561
1562 if (buf_size && sumlen > buf_size)
1563 free(sumptr);
Ilya Yanoka87011e2009-08-12 16:42:48 +04001564 if (ret < 0) {
1565 free(buf);
1566 jffs2_free_cache(part);
Ilya Yanoka933bd62008-11-13 19:49:35 +03001567 return 0;
Ilya Yanoka87011e2009-08-12 16:42:48 +04001568 }
Ilya Yanoka933bd62008-11-13 19:49:35 +03001569 if (ret)
1570 continue;
1571
1572 }
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001573#endif /* CONFIG_JFFS2_SUMMARY */
Ilya Yanoka933bd62008-11-13 19:49:35 +03001574
1575 buf_len = EMPTY_SCAN_SIZE(part->sector_size);
1576
Ilya Yanokddb02692008-11-13 19:49:33 +03001577 get_fl_mem((u32)part->offset + buf_ofs, buf_len, buf);
Stuart Wood471a2f02008-06-02 16:40:08 -04001578
Ilya Yanokddb02692008-11-13 19:49:33 +03001579 /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
1580 ofs = 0;
1581
1582 /* Scan only 4KiB of 0xFF before declaring it's empty */
1583 while (ofs < EMPTY_SCAN_SIZE(part->sector_size) &&
1584 *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
1585 ofs += 4;
1586
1587 if (ofs == EMPTY_SCAN_SIZE(part->sector_size))
1588 continue;
1589
1590 ofs += sector_ofs;
1591 prevofs = ofs - 1;
Mark Tomlinsonef619822015-07-01 16:38:26 +12001592 /*
1593 * Set buf_size down to the minimum size required.
1594 * This prevents reading in chunks of flash data unnecessarily.
1595 */
1596 buf_size = sizeof(union jffs2_node_union);
Ilya Yanokddb02692008-11-13 19:49:33 +03001597
1598 scan_more:
1599 while (ofs < sector_ofs + part->sector_size) {
1600 if (ofs == prevofs) {
1601 printf("offset %08x already seen, skip\n", ofs);
1602 ofs += 4;
1603 counter4++;
1604 continue;
1605 }
1606 prevofs = ofs;
1607 if (sector_ofs + part->sector_size <
Petr Borsodia61d1d12020-05-07 12:25:55 +02001608 ofs + sizeof(struct jffs2_unknown_node))
Ilya Yanokddb02692008-11-13 19:49:33 +03001609 break;
Petr Borsodia61d1d12020-05-07 12:25:55 +02001610 if (buf_ofs + buf_len <
1611 ofs + sizeof(struct jffs2_unknown_node)) {
Ilya Yanokddb02692008-11-13 19:49:33 +03001612 buf_len = min_t(uint32_t, buf_size, sector_ofs
1613 + part->sector_size - ofs);
1614 get_fl_mem((u32)part->offset + ofs, buf_len,
1615 buf);
1616 buf_ofs = ofs;
1617 }
1618
Petr Borsodia61d1d12020-05-07 12:25:55 +02001619 node = (union jffs2_node_union *)&buf[ofs - buf_ofs];
Ilya Yanokddb02692008-11-13 19:49:33 +03001620
1621 if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
1622 uint32_t inbuf_ofs;
Wolfgang Denkbf913c12011-10-05 22:58:11 +02001623 uint32_t scan_end;
Ilya Yanokddb02692008-11-13 19:49:33 +03001624
Ilya Yanokddb02692008-11-13 19:49:33 +03001625 ofs += 4;
1626 scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(
1627 part->sector_size)/8,
1628 buf_len);
1629 more_empty:
1630 inbuf_ofs = ofs - buf_ofs;
1631 while (inbuf_ofs < scan_end) {
1632 if (*(uint32_t *)(&buf[inbuf_ofs]) !=
1633 0xffffffff)
1634 goto scan_more;
1635
1636 inbuf_ofs += 4;
1637 ofs += 4;
1638 }
1639 /* Ran off end. */
Mark Tomlinson56a7f482015-07-01 16:38:28 +12001640 /*
1641 * If this sector had a clean marker at the
1642 * beginning, and immediately following this
1643 * have been a bunch of FF bytes, treat the
1644 * entire sector as empty.
1645 */
1646 if (clean_sector)
1647 break;
Ilya Yanokddb02692008-11-13 19:49:33 +03001648
1649 /* See how much more there is to read in this
1650 * eraseblock...
1651 */
1652 buf_len = min_t(uint32_t, buf_size,
1653 sector_ofs +
1654 part->sector_size - ofs);
1655 if (!buf_len) {
1656 /* No more to read. Break out of main
1657 * loop without marking this range of
1658 * empty space as dirty (because it's
1659 * not)
1660 */
1661 break;
1662 }
1663 scan_end = buf_len;
1664 get_fl_mem((u32)part->offset + ofs, buf_len,
1665 buf);
1666 buf_ofs = ofs;
1667 goto more_empty;
1668 }
Mark Tomlinson56a7f482015-07-01 16:38:28 +12001669 /*
1670 * Found something not erased in the sector, so reset
1671 * the 'clean_sector' flag.
1672 */
1673 clean_sector = 0;
Petr Borsodia61d1d12020-05-07 12:25:55 +02001674 if (node->u.magic != JFFS2_MAGIC_BITMASK) {
Ilya Yanokddb02692008-11-13 19:49:33 +03001675 ofs += 4;
1676 counter4++;
1677 continue;
1678 }
Petr Borsodia61d1d12020-05-07 12:25:55 +02001679
1680 crcnode.magic = node->u.magic;
1681 crcnode.nodetype = node->u.nodetype | JFFS2_NODE_ACCURATE;
1682 crcnode.totlen = node->u.totlen;
1683 crcnode.hdr_crc = node->u.hdr_crc;
1684 if (!hdr_crc(&crcnode)) {
1685 ofs += 4;
1686 counter4++;
1687 continue;
1688 }
1689
1690 if (ofs + node->u.totlen > sector_ofs + part->sector_size) {
Ilya Yanokddb02692008-11-13 19:49:33 +03001691 ofs += 4;
1692 counter4++;
1693 continue;
1694 }
Petr Borsodia61d1d12020-05-07 12:25:55 +02001695
1696 if (!(node->u.nodetype & JFFS2_NODE_ACCURATE)) {
1697 DEBUGF("Obsolete node type: %x len %d offset 0x%x\n",
1698 node->u.nodetype, node->u.totlen, ofs);
1699 ofs += ((node->u.totlen + 3) & ~3);
1700 counterF++;
1701 continue;
1702 }
1703
wdenkfe8c2802002-11-03 00:38:21 +00001704 /* if its a fragment add it */
Petr Borsodia61d1d12020-05-07 12:25:55 +02001705 switch (node->u.nodetype) {
Ilya Yanokddb02692008-11-13 19:49:33 +03001706 case JFFS2_NODETYPE_INODE:
Petr Borsodia61d1d12020-05-07 12:25:55 +02001707 if (buf_ofs + buf_len <
1708 ofs + sizeof(struct jffs2_raw_inode)) {
Mark Tomlinsonef619822015-07-01 16:38:26 +12001709 buf_len = min_t(uint32_t,
1710 sizeof(struct jffs2_raw_inode),
1711 sector_ofs +
1712 part->sector_size -
1713 ofs);
Ilya Yanokddb02692008-11-13 19:49:33 +03001714 get_fl_mem((u32)part->offset + ofs,
1715 buf_len, buf);
1716 buf_ofs = ofs;
1717 node = (void *)buf;
1718 }
Mark Tomlinsonef619822015-07-01 16:38:26 +12001719 if (!inode_crc((struct jffs2_raw_inode *)node))
1720 break;
Ilya Yanokddb02692008-11-13 19:49:33 +03001721
Petr Borsodi80c25b12020-05-07 12:25:56 +02001722 b = insert_node(&pL->frag);
1723 if (!b) {
Ilya Yanoka87011e2009-08-12 16:42:48 +04001724 free(buf);
1725 jffs2_free_cache(part);
wdenkfe8c2802002-11-03 00:38:21 +00001726 return 0;
Ilya Yanoka87011e2009-08-12 16:42:48 +04001727 }
Petr Borsodi80c25b12020-05-07 12:25:56 +02001728 b->offset = (u32)part->offset + ofs;
1729 b->version = node->i.version;
1730 b->ino = node->i.ino;
Petr Borsodia61d1d12020-05-07 12:25:55 +02001731 if (max_totlen < node->u.totlen)
1732 max_totlen = node->u.totlen;
Ilya Yanokddb02692008-11-13 19:49:33 +03001733 break;
1734 case JFFS2_NODETYPE_DIRENT:
1735 if (buf_ofs + buf_len < ofs + sizeof(struct
1736 jffs2_raw_dirent) +
1737 ((struct
1738 jffs2_raw_dirent *)
1739 node)->nsize) {
Mark Tomlinsonef619822015-07-01 16:38:26 +12001740 buf_len = min_t(uint32_t,
Petr Borsodia61d1d12020-05-07 12:25:55 +02001741 node->u.totlen,
Mark Tomlinsonef619822015-07-01 16:38:26 +12001742 sector_ofs +
1743 part->sector_size -
1744 ofs);
Ilya Yanokddb02692008-11-13 19:49:33 +03001745 get_fl_mem((u32)part->offset + ofs,
1746 buf_len, buf);
1747 buf_ofs = ofs;
1748 node = (void *)buf;
wdenk8886a662004-04-18 19:43:36 +00001749 }
Ilya Yanokddb02692008-11-13 19:49:33 +03001750
1751 if (!dirent_crc((struct jffs2_raw_dirent *)
1752 node) ||
1753 !dirent_name_crc(
1754 (struct
1755 jffs2_raw_dirent *)
1756 node))
1757 break;
wdenk7ad5e4c2004-04-25 15:41:35 +00001758 if (! (counterN%100))
wdenk42c05472004-03-23 22:14:11 +00001759 puts ("\b\b. ");
Petr Borsodi80c25b12020-05-07 12:25:56 +02001760 b = insert_node(&pL->dir);
1761 if (!b) {
Ilya Yanoka87011e2009-08-12 16:42:48 +04001762 free(buf);
1763 jffs2_free_cache(part);
wdenkfe8c2802002-11-03 00:38:21 +00001764 return 0;
Ilya Yanoka87011e2009-08-12 16:42:48 +04001765 }
Petr Borsodi80c25b12020-05-07 12:25:56 +02001766 b->offset = (u32)part->offset + ofs;
1767 b->version = node->d.version;
1768 b->pino = node->d.pino;
Petr Borsodia61d1d12020-05-07 12:25:55 +02001769 if (max_totlen < node->u.totlen)
1770 max_totlen = node->u.totlen;
wdenkfe8c2802002-11-03 00:38:21 +00001771 counterN++;
Ilya Yanokddb02692008-11-13 19:49:33 +03001772 break;
1773 case JFFS2_NODETYPE_CLEANMARKER:
Petr Borsodia61d1d12020-05-07 12:25:55 +02001774 if (node->u.totlen != sizeof(struct jffs2_unknown_node))
wdenk6b58f332003-03-14 20:47:52 +00001775 printf("OOPS Cleanmarker has bad size "
Wolfgang Denk509cd072008-07-14 15:06:35 +02001776 "%d != %zu\n",
Petr Borsodia61d1d12020-05-07 12:25:55 +02001777 node->u.totlen,
wdenk6b58f332003-03-14 20:47:52 +00001778 sizeof(struct jffs2_unknown_node));
Petr Borsodia61d1d12020-05-07 12:25:55 +02001779 if (node->u.totlen ==
1780 sizeof(struct jffs2_unknown_node) &&
1781 ofs == sector_ofs) {
Mark Tomlinson56a7f482015-07-01 16:38:28 +12001782 /*
1783 * Found a CLEANMARKER at the beginning
1784 * of the sector. It's in the correct
1785 * place with correct size and CRC.
1786 */
1787 clean_sector = 1;
1788 }
Ilya Yanokddb02692008-11-13 19:49:33 +03001789 break;
1790 case JFFS2_NODETYPE_PADDING:
Petr Borsodia61d1d12020-05-07 12:25:55 +02001791 if (node->u.totlen <
1792 sizeof(struct jffs2_unknown_node))
wdenk2c9b05d2003-09-10 22:30:53 +00001793 printf("OOPS Padding has bad size "
Wolfgang Denk509cd072008-07-14 15:06:35 +02001794 "%d < %zu\n",
Petr Borsodia61d1d12020-05-07 12:25:55 +02001795 node->u.totlen,
wdenk2c9b05d2003-09-10 22:30:53 +00001796 sizeof(struct jffs2_unknown_node));
Ilya Yanokddb02692008-11-13 19:49:33 +03001797 break;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001798 case JFFS2_NODETYPE_SUMMARY:
1799 break;
Ilya Yanokddb02692008-11-13 19:49:33 +03001800 default:
Wolfgang Denk509cd072008-07-14 15:06:35 +02001801 printf("Unknown node type: %x len %d offset 0x%x\n",
Petr Borsodia61d1d12020-05-07 12:25:55 +02001802 node->u.nodetype,
1803 node->u.totlen, ofs);
wdenkfe8c2802002-11-03 00:38:21 +00001804 }
Petr Borsodia61d1d12020-05-07 12:25:55 +02001805 ofs += ((node->u.totlen + 3) & ~3);
wdenkfe8c2802002-11-03 00:38:21 +00001806 counterF++;
wdenkfe8c2802002-11-03 00:38:21 +00001807 }
wdenkfe8c2802002-11-03 00:38:21 +00001808 }
1809
Ilya Yanokddb02692008-11-13 19:49:33 +03001810 free(buf);
Mark Tomlinsonefdc2a62015-07-01 16:38:29 +12001811#if defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS)
1812 /*
1813 * Sort the lists.
1814 */
1815 sort_list(&pL->frag);
1816 sort_list(&pL->dir);
1817#endif
wdenkfe8c2802002-11-03 00:38:21 +00001818 putstr("\b\b done.\r\n"); /* close off the dots */
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001819
1820 /* We don't care if malloc failed - then each read operation will
1821 * allocate its own buffer as necessary (NAND) or will read directly
1822 * from flash (NOR).
1823 */
1824 pL->readbuf = malloc(max_totlen);
1825
wdenkfe8c2802002-11-03 00:38:21 +00001826 /* turn the lcd back on. */
1827 /* splash(); */
1828
1829#if 0
wdenk6b58f332003-03-14 20:47:52 +00001830 putLabeledWord("dir entries = ", pL->dir.listCount);
1831 putLabeledWord("frag entries = ", pL->frag.listCount);
wdenkfe8c2802002-11-03 00:38:21 +00001832 putLabeledWord("+4 increments = ", counter4);
1833 putLabeledWord("+file_offset increments = ", counterF);
1834
1835#endif
1836
wdenk6b58f332003-03-14 20:47:52 +00001837#ifdef DEBUG_DIRENTS
1838 dump_dirents(pL);
1839#endif
wdenkfe8c2802002-11-03 00:38:21 +00001840
wdenk6b58f332003-03-14 20:47:52 +00001841#ifdef DEBUG_FRAGMENTS
1842 dump_fragments(pL);
1843#endif
wdenkfe8c2802002-11-03 00:38:21 +00001844
wdenkfe8c2802002-11-03 00:38:21 +00001845 /* give visual feedback that we are done scanning the flash */
1846 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
1847 return 1;
1848}
1849
wdenkfe8c2802002-11-03 00:38:21 +00001850static u32
1851jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
1852{
1853 struct b_node *b;
wdenk8886a662004-04-18 19:43:36 +00001854 struct jffs2_raw_inode ojNode;
wdenkfe8c2802002-11-03 00:38:21 +00001855 struct jffs2_raw_inode *jNode;
1856 int i;
1857
wdenkfe8c2802002-11-03 00:38:21 +00001858 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1859 piL->compr_info[i].num_frags = 0;
1860 piL->compr_info[i].compr_sum = 0;
1861 piL->compr_info[i].decompr_sum = 0;
1862 }
1863
wdenk6b58f332003-03-14 20:47:52 +00001864 b = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +00001865 while (b) {
wdenk8886a662004-04-18 19:43:36 +00001866 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1867 sizeof(ojNode), &ojNode);
wdenkfe8c2802002-11-03 00:38:21 +00001868 if (jNode->compr < JFFS2_NUM_COMPR) {
1869 piL->compr_info[jNode->compr].num_frags++;
1870 piL->compr_info[jNode->compr].compr_sum += jNode->csize;
1871 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
1872 }
1873 b = b->next;
1874 }
1875 return 0;
1876}
1877
wdenkfe8c2802002-11-03 00:38:21 +00001878static struct b_lists *
1879jffs2_get_list(struct part_info * part, const char *who)
1880{
Wolfgang Denk47f57792005-08-08 01:03:24 +02001881 /* copy requested part_info struct pointer to global location */
1882 current_part = part;
1883
wdenkfe8c2802002-11-03 00:38:21 +00001884 if (jffs2_1pass_rescan_needed(part)) {
1885 if (!jffs2_1pass_build_lists(part)) {
1886 printf("%s: Failed to scan JFFSv2 file structure\n", who);
1887 return NULL;
1888 }
1889 }
1890 return (struct b_lists *)part->jffs2_priv;
1891}
1892
wdenkfe8c2802002-11-03 00:38:21 +00001893/* Print directory / file contents */
1894u32
1895jffs2_1pass_ls(struct part_info * part, const char *fname)
1896{
1897 struct b_lists *pl;
Wolfgang Denk79117cd2005-08-16 09:32:45 +02001898 long ret = 1;
wdenkfe8c2802002-11-03 00:38:21 +00001899 u32 inode;
1900
wdenk6b58f332003-03-14 20:47:52 +00001901 if (! (pl = jffs2_get_list(part, "ls")))
wdenkfe8c2802002-11-03 00:38:21 +00001902 return 0;
1903
1904 if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
1905 putstr("ls: Failed to scan jffs2 file structure\r\n");
1906 return 0;
1907 }
wdenk7ad5e4c2004-04-25 15:41:35 +00001908
wdenkfe8c2802002-11-03 00:38:21 +00001909#if 0
1910 putLabeledWord("found file at inode = ", inode);
1911 putLabeledWord("read_inode returns = ", ret);
1912#endif
wdenk7ad5e4c2004-04-25 15:41:35 +00001913
1914 return ret;
wdenkfe8c2802002-11-03 00:38:21 +00001915}
1916
wdenkfe8c2802002-11-03 00:38:21 +00001917/* Load a file from flash into memory. fname can be a full path */
1918u32
1919jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
1920{
1921
1922 struct b_lists *pl;
Wolfgang Denk79117cd2005-08-16 09:32:45 +02001923 long ret = 1;
wdenkfe8c2802002-11-03 00:38:21 +00001924 u32 inode;
1925
1926 if (! (pl = jffs2_get_list(part, "load")))
1927 return 0;
1928
1929 if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
1930 putstr("load: Failed to find inode\r\n");
1931 return 0;
1932 }
1933
1934 /* Resolve symlinks */
1935 if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
1936 putstr("load: Failed to resolve inode structure\r\n");
1937 return 0;
1938 }
1939
1940 if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
1941 putstr("load: Failed to read inode\r\n");
1942 return 0;
1943 }
1944
1945 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1946 (unsigned long) dest, ret);
1947 return ret;
1948}
1949
1950/* Return information about the fs on this partition */
1951u32
1952jffs2_1pass_info(struct part_info * part)
1953{
1954 struct b_jffs2_info info;
1955 struct b_lists *pl;
1956 int i;
1957
1958 if (! (pl = jffs2_get_list(part, "info")))
1959 return 0;
1960
1961 jffs2_1pass_fill_info(pl, &info);
wdenk6b58f332003-03-14 20:47:52 +00001962 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
wdenk42c05472004-03-23 22:14:11 +00001963 printf ("Compression: %s\n"
1964 "\tfrag count: %d\n"
1965 "\tcompressed sum: %d\n"
1966 "\tuncompressed sum: %d\n",
1967 compr_names[i],
1968 info.compr_info[i].num_frags,
1969 info.compr_info[i].compr_sum,
1970 info.compr_info[i].decompr_sum);
wdenkfe8c2802002-11-03 00:38:21 +00001971 }
1972 return 1;
1973}