blob: 5b7d7f4ae881cee34d97e000b8af651446e2bd0b [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
113
wdenkfe8c2802002-11-03 00:38:21 +0000114#include <config.h>
115#include <malloc.h>
Tom Rini3cd84d62013-12-05 14:48:38 -0500116#include <div64.h>
Tom Rini7c4734d2016-03-27 14:48:36 -0400117#include <linux/compiler.h>
wdenkfe8c2802002-11-03 00:38:21 +0000118#include <linux/stat.h>
119#include <linux/time.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -0700120#include <u-boot/crc.h>
Stuart Wood471a2f02008-06-02 16:40:08 -0400121#include <watchdog.h>
wdenkfe8c2802002-11-03 00:38:21 +0000122#include <jffs2/jffs2.h>
123#include <jffs2/jffs2_1pass.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +0000124#include <linux/compat.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +0900125#include <linux/errno.h>
wdenkfe8c2802002-11-03 00:38:21 +0000126
127#include "jffs2_private.h"
128
wdenk6b58f332003-03-14 20:47:52 +0000129
Wolfgang Denka1be4762008-05-20 16:00:29 +0200130#define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */
131#define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */
wdenkfe8c2802002-11-03 00:38:21 +0000132
wdenk6b58f332003-03-14 20:47:52 +0000133/* Debugging switches */
134#undef DEBUG_DIRENTS /* print directory entry list after scan */
135#undef DEBUG_FRAGMENTS /* print fragment list after scan */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200136#undef DEBUG /* enable debugging messages */
wdenkfe8c2802002-11-03 00:38:21 +0000137
wdenk6b58f332003-03-14 20:47:52 +0000138
wdenkfe8c2802002-11-03 00:38:21 +0000139#ifdef DEBUG
140# define DEBUGF(fmt,args...) printf(fmt ,##args)
141#else
142# define DEBUGF(fmt,args...)
143#endif
144
Ilya Yanoka933bd62008-11-13 19:49:35 +0300145#include "summary.h"
146
Wolfgang Denk47f57792005-08-08 01:03:24 +0200147/* keeps pointer to currentlu processed partition */
148static struct part_info *current_part;
wdenk8886a662004-04-18 19:43:36 +0000149
Wolfgang Denk15888b42007-07-05 17:56:27 +0200150#if (defined(CONFIG_JFFS2_NAND) && \
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500151 defined(CONFIG_CMD_NAND) )
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100152#include <nand.h>
wdenk8886a662004-04-18 19:43:36 +0000153/*
154 * Support for jffs2 on top of NAND-flash
155 *
156 * NAND memory isn't mapped in processor's address space,
157 * so data should be fetched from flash before
158 * being processed. This is exactly what functions declared
159 * here do.
160 *
161 */
162
wdenk8886a662004-04-18 19:43:36 +0000163#define NAND_PAGE_SIZE 512
164#define NAND_PAGE_SHIFT 9
165#define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1))
166
167#ifndef NAND_CACHE_PAGES
168#define NAND_CACHE_PAGES 16
169#endif
170#define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE)
171
172static u8* nand_cache = NULL;
173static u32 nand_cache_off = (u32)-1;
wdenk8886a662004-04-18 19:43:36 +0000174
175static int read_nand_cached(u32 off, u32 size, u_char *buf)
176{
Wolfgang Denk47f57792005-08-08 01:03:24 +0200177 struct mtdids *id = current_part->dev->id;
Grygorii Strashkoac8f8e32017-06-26 19:12:57 -0500178 struct mtd_info *mtd;
wdenk8886a662004-04-18 19:43:36 +0000179 u32 bytes_read = 0;
Marian Balakowicz6a076752006-04-08 19:08:06 +0200180 size_t retlen;
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300181 size_t toread;
wdenk8886a662004-04-18 19:43:36 +0000182 int cpy_bytes;
183
Grygorii Strashkoac8f8e32017-06-26 19:12:57 -0500184 mtd = get_nand_dev_by_index(id->num);
185 if (!mtd)
186 return -1;
187
wdenk8886a662004-04-18 19:43:36 +0000188 while (bytes_read < size) {
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300189 retlen = NAND_CACHE_SIZE;
190 if( nand_cache_off + retlen > mtd->size )
191 retlen = mtd->size - nand_cache_off;
192
wdenk8886a662004-04-18 19:43:36 +0000193 if ((off + bytes_read < nand_cache_off) ||
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300194 (off + bytes_read >= nand_cache_off + retlen)) {
wdenk8886a662004-04-18 19:43:36 +0000195 nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK;
196 if (!nand_cache) {
197 /* This memory never gets freed but 'cause
198 it's a bootloader, nobody cares */
wdenk12490652004-04-18 21:13:41 +0000199 nand_cache = malloc(NAND_CACHE_SIZE);
200 if (!nand_cache) {
wdenk8886a662004-04-18 19:43:36 +0000201 printf("read_nand_cached: can't alloc cache size %d bytes\n",
202 NAND_CACHE_SIZE);
203 return -1;
204 }
205 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100206
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300207 toread = NAND_CACHE_SIZE;
208 if( nand_cache_off + toread > mtd->size )
209 toread = mtd->size - nand_cache_off;
210
211 retlen = toread;
Grygorii Strashkoac8f8e32017-06-26 19:12:57 -0500212 if (nand_read(mtd, nand_cache_off,
Engling, Uwee5e23ee2017-10-10 14:20:55 +0000213 &retlen, nand_cache) < 0 ||
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300214 retlen != toread) {
wdenk8886a662004-04-18 19:43:36 +0000215 printf("read_nand_cached: error reading nand off %#x size %d bytes\n",
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300216 nand_cache_off, toread);
wdenk8886a662004-04-18 19:43:36 +0000217 return -1;
218 }
219 }
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300220 cpy_bytes = nand_cache_off + retlen - (off + bytes_read);
wdenk8886a662004-04-18 19:43:36 +0000221 if (cpy_bytes > size - bytes_read)
222 cpy_bytes = size - bytes_read;
223 memcpy(buf + bytes_read,
224 nand_cache + off + bytes_read - nand_cache_off,
225 cpy_bytes);
226 bytes_read += cpy_bytes;
227 }
228 return bytes_read;
229}
230
Wolfgang Denk47f57792005-08-08 01:03:24 +0200231static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf)
wdenk8886a662004-04-18 19:43:36 +0000232{
233 u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size);
234
235 if (NULL == buf) {
Wolfgang Denk47f57792005-08-08 01:03:24 +0200236 printf("get_fl_mem_nand: can't alloc %d bytes\n", size);
wdenk8886a662004-04-18 19:43:36 +0000237 return NULL;
238 }
239 if (read_nand_cached(off, size, buf) < 0) {
wdenkf248ebc2004-05-05 19:44:41 +0000240 if (!ext_buf)
241 free(buf);
wdenk8886a662004-04-18 19:43:36 +0000242 return NULL;
243 }
244
245 return buf;
246}
247
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300248static void *get_node_mem_nand(u32 off, void *ext_buf)
wdenk8886a662004-04-18 19:43:36 +0000249{
250 struct jffs2_unknown_node node;
251 void *ret = NULL;
252
Wolfgang Denk47f57792005-08-08 01:03:24 +0200253 if (NULL == get_fl_mem_nand(off, sizeof(node), &node))
wdenk8886a662004-04-18 19:43:36 +0000254 return NULL;
255
Wolfgang Denk47f57792005-08-08 01:03:24 +0200256 if (!(ret = get_fl_mem_nand(off, node.magic ==
wdenk8886a662004-04-18 19:43:36 +0000257 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300258 ext_buf))) {
wdenk8886a662004-04-18 19:43:36 +0000259 printf("off = %#x magic %#x type %#x node.totlen = %d\n",
260 off, node.magic, node.nodetype, node.totlen);
261 }
262 return ret;
263}
264
Wolfgang Denk47f57792005-08-08 01:03:24 +0200265static void put_fl_mem_nand(void *buf)
wdenk8886a662004-04-18 19:43:36 +0000266{
267 free(buf);
268}
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500269#endif
Wolfgang Denk47f57792005-08-08 01:03:24 +0200270
Kyungmin Park25383742008-08-27 14:45:20 +0900271#if defined(CONFIG_CMD_ONENAND)
272
273#include <linux/mtd/mtd.h>
274#include <linux/mtd/onenand.h>
275#include <onenand_uboot.h>
276
277#define ONENAND_PAGE_SIZE 2048
278#define ONENAND_PAGE_SHIFT 11
279#define ONENAND_PAGE_MASK (~(ONENAND_PAGE_SIZE-1))
280
281#ifndef ONENAND_CACHE_PAGES
282#define ONENAND_CACHE_PAGES 4
283#endif
284#define ONENAND_CACHE_SIZE (ONENAND_CACHE_PAGES*ONENAND_PAGE_SIZE)
285
286static u8* onenand_cache;
287static u32 onenand_cache_off = (u32)-1;
288
289static int read_onenand_cached(u32 off, u32 size, u_char *buf)
290{
291 u32 bytes_read = 0;
292 size_t retlen;
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300293 size_t toread;
Kyungmin Park25383742008-08-27 14:45:20 +0900294 int cpy_bytes;
295
296 while (bytes_read < size) {
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300297 retlen = ONENAND_CACHE_SIZE;
298 if( onenand_cache_off + retlen > onenand_mtd.size )
299 retlen = onenand_mtd.size - onenand_cache_off;
300
Kyungmin Park25383742008-08-27 14:45:20 +0900301 if ((off + bytes_read < onenand_cache_off) ||
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300302 (off + bytes_read >= onenand_cache_off + retlen)) {
Kyungmin Park25383742008-08-27 14:45:20 +0900303 onenand_cache_off = (off + bytes_read) & ONENAND_PAGE_MASK;
304 if (!onenand_cache) {
305 /* This memory never gets freed but 'cause
306 it's a bootloader, nobody cares */
307 onenand_cache = malloc(ONENAND_CACHE_SIZE);
308 if (!onenand_cache) {
309 printf("read_onenand_cached: can't alloc cache size %d bytes\n",
310 ONENAND_CACHE_SIZE);
311 return -1;
312 }
313 }
314
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300315 toread = ONENAND_CACHE_SIZE;
316 if( onenand_cache_off + toread > onenand_mtd.size )
317 toread = onenand_mtd.size - onenand_cache_off;
318 retlen = toread;
Kyungmin Park25383742008-08-27 14:45:20 +0900319 if (onenand_read(&onenand_mtd, onenand_cache_off, retlen,
Engling, Uwee5e23ee2017-10-10 14:20:55 +0000320 &retlen, onenand_cache) < 0 ||
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300321 retlen != toread) {
Kyungmin Park25383742008-08-27 14:45:20 +0900322 printf("read_onenand_cached: error reading nand off %#x size %d bytes\n",
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300323 onenand_cache_off, toread);
Kyungmin Park25383742008-08-27 14:45:20 +0900324 return -1;
325 }
326 }
Wagner Popov dos Santosa943bef2021-02-22 23:30:58 -0300327 cpy_bytes = onenand_cache_off + retlen - (off + bytes_read);
Kyungmin Park25383742008-08-27 14:45:20 +0900328 if (cpy_bytes > size - bytes_read)
329 cpy_bytes = size - bytes_read;
330 memcpy(buf + bytes_read,
331 onenand_cache + off + bytes_read - onenand_cache_off,
332 cpy_bytes);
333 bytes_read += cpy_bytes;
334 }
335 return bytes_read;
336}
337
338static void *get_fl_mem_onenand(u32 off, u32 size, void *ext_buf)
339{
340 u_char *buf = ext_buf ? (u_char *)ext_buf : (u_char *)malloc(size);
341
342 if (NULL == buf) {
343 printf("get_fl_mem_onenand: can't alloc %d bytes\n", size);
344 return NULL;
345 }
346 if (read_onenand_cached(off, size, buf) < 0) {
347 if (!ext_buf)
348 free(buf);
349 return NULL;
350 }
351
352 return buf;
353}
354
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300355static void *get_node_mem_onenand(u32 off, void *ext_buf)
Kyungmin Park25383742008-08-27 14:45:20 +0900356{
357 struct jffs2_unknown_node node;
358 void *ret = NULL;
359
360 if (NULL == get_fl_mem_onenand(off, sizeof(node), &node))
361 return NULL;
362
363 ret = get_fl_mem_onenand(off, node.magic ==
364 JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node),
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300365 ext_buf);
Kyungmin Park25383742008-08-27 14:45:20 +0900366 if (!ret) {
367 printf("off = %#x magic %#x type %#x node.totlen = %d\n",
368 off, node.magic, node.nodetype, node.totlen);
369 }
370 return ret;
371}
372
373
374static void put_fl_mem_onenand(void *buf)
375{
376 free(buf);
377}
378#endif
379
Wolfgang Denk47f57792005-08-08 01:03:24 +0200380
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500381#if defined(CONFIG_CMD_FLASH)
Tom Rini063c9382022-07-23 13:05:03 -0400382#include <flash.h>
383
Wolfgang Denk47f57792005-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 Yanoke7fd6712008-11-13 19:49:34 +0300390static inline void *get_fl_mem_nor(u32 off, u32 size, void *ext_buf)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200391{
392 u32 addr = off;
393 struct mtdids *id = current_part->dev->id;
394
Wolfgang Denk47f57792005-08-08 01:03:24 +0200395 flash_info_t *flash = &flash_info[id->num];
396
397 addr += flash->start[0];
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300398 if (ext_buf) {
399 memcpy(ext_buf, (void *)addr, size);
400 return ext_buf;
401 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200402 return (void*)addr;
403}
404
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300405static inline void *get_node_mem_nor(u32 off, void *ext_buf)
Ilya Yanokddb02692008-11-13 19:49:33 +0300406{
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300407 struct jffs2_unknown_node *pNode;
Ilya Yanokddb02692008-11-13 19:49:33 +0300408
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300409 /* pNode will point directly to flash - don't provide external buffer
410 and don't care about size */
411 pNode = get_fl_mem_nor(off, 0, NULL);
412 return (void *)get_fl_mem_nor(off, pNode->magic == JFFS2_MAGIC_BITMASK ?
413 pNode->totlen : sizeof(*pNode), ext_buf);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200414}
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500415#endif
wdenk8886a662004-04-18 19:43:36 +0000416
wdenk8886a662004-04-18 19:43:36 +0000417
Wolfgang Denk47f57792005-08-08 01:03:24 +0200418/*
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200419 * Generic jffs2 raw memory and node read routines.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200420 *
421 */
wdenk8886a662004-04-18 19:43:36 +0000422static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf)
423{
Wolfgang Denk47f57792005-08-08 01:03:24 +0200424 struct mtdids *id = current_part->dev->id;
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200425
Heiko Schocherd64a8132010-03-24 13:22:50 +0100426 switch(id->type) {
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500427#if defined(CONFIG_CMD_FLASH)
Heiko Schocherd64a8132010-03-24 13:22:50 +0100428 case MTD_DEV_TYPE_NOR:
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300429 return get_fl_mem_nor(off, size, ext_buf);
Heiko Schocherd64a8132010-03-24 13:22:50 +0100430 break;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200431#endif
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500432#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
Heiko Schocherd64a8132010-03-24 13:22:50 +0100433 case MTD_DEV_TYPE_NAND:
Wolfgang Denk47f57792005-08-08 01:03:24 +0200434 return get_fl_mem_nand(off, size, ext_buf);
Heiko Schocherd64a8132010-03-24 13:22:50 +0100435 break;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200436#endif
Kyungmin Park25383742008-08-27 14:45:20 +0900437#if defined(CONFIG_CMD_ONENAND)
Heiko Schocherd64a8132010-03-24 13:22:50 +0100438 case MTD_DEV_TYPE_ONENAND:
Kyungmin Park25383742008-08-27 14:45:20 +0900439 return get_fl_mem_onenand(off, size, ext_buf);
Heiko Schocherd64a8132010-03-24 13:22:50 +0100440 break;
Kyungmin Park25383742008-08-27 14:45:20 +0900441#endif
Heiko Schocherd64a8132010-03-24 13:22:50 +0100442 default:
443 printf("get_fl_mem: unknown device type, " \
444 "using raw offset!\n");
445 }
wdenk8886a662004-04-18 19:43:36 +0000446 return (void*)off;
447}
448
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300449static inline void *get_node_mem(u32 off, void *ext_buf)
wdenk8886a662004-04-18 19:43:36 +0000450{
Wolfgang Denk47f57792005-08-08 01:03:24 +0200451 struct mtdids *id = current_part->dev->id;
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200452
Heiko Schocherd64a8132010-03-24 13:22:50 +0100453 switch(id->type) {
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500454#if defined(CONFIG_CMD_FLASH)
Heiko Schocherd64a8132010-03-24 13:22:50 +0100455 case MTD_DEV_TYPE_NOR:
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300456 return get_node_mem_nor(off, ext_buf);
Heiko Schocherd64a8132010-03-24 13:22:50 +0100457 break;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200458#endif
Wolfgang Denk15888b42007-07-05 17:56:27 +0200459#if defined(CONFIG_JFFS2_NAND) && \
Jon Loeliger80eb47c2007-07-09 17:56:50 -0500460 defined(CONFIG_CMD_NAND)
Heiko Schocherd64a8132010-03-24 13:22:50 +0100461 case MTD_DEV_TYPE_NAND:
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300462 return get_node_mem_nand(off, ext_buf);
Heiko Schocherd64a8132010-03-24 13:22:50 +0100463 break;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200464#endif
Kyungmin Park25383742008-08-27 14:45:20 +0900465#if defined(CONFIG_CMD_ONENAND)
Heiko Schocherd64a8132010-03-24 13:22:50 +0100466 case MTD_DEV_TYPE_ONENAND:
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300467 return get_node_mem_onenand(off, ext_buf);
Heiko Schocherd64a8132010-03-24 13:22:50 +0100468 break;
Kyungmin Park25383742008-08-27 14:45:20 +0900469#endif
Heiko Schocherd64a8132010-03-24 13:22:50 +0100470 default:
471 printf("get_fl_mem: unknown device type, " \
472 "using raw offset!\n");
473 }
wdenk8886a662004-04-18 19:43:36 +0000474 return (void*)off;
475}
476
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300477static inline void put_fl_mem(void *buf, void *ext_buf)
wdenk8886a662004-04-18 19:43:36 +0000478{
Wolfgang Denk47f57792005-08-08 01:03:24 +0200479 struct mtdids *id = current_part->dev->id;
wdenk8886a662004-04-18 19:43:36 +0000480
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300481 /* If buf is the same as ext_buf, it was provided by the caller -
482 we shouldn't free it then. */
483 if (buf == ext_buf)
484 return;
Scott Woodc97ba112008-10-31 13:51:12 -0500485 switch (id->type) {
486#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
487 case MTD_DEV_TYPE_NAND:
Wolfgang Denk47f57792005-08-08 01:03:24 +0200488 return put_fl_mem_nand(buf);
489#endif
Kyungmin Park25383742008-08-27 14:45:20 +0900490#if defined(CONFIG_CMD_ONENAND)
Scott Woodc97ba112008-10-31 13:51:12 -0500491 case MTD_DEV_TYPE_ONENAND:
Kyungmin Park25383742008-08-27 14:45:20 +0900492 return put_fl_mem_onenand(buf);
493#endif
Scott Woodc97ba112008-10-31 13:51:12 -0500494 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200495}
wdenk6b58f332003-03-14 20:47:52 +0000496
497/* Compression names */
498static char *compr_names[] = {
499 "NONE",
500 "ZERO",
501 "RTIME",
502 "RUBINMIPS",
503 "COPY",
504 "DYNRUBIN",
wdenke84ec902005-05-05 00:04:14 +0000505 "ZLIB",
Wolfgang Denk0eded4f2010-01-15 11:10:33 +0100506#if defined(CONFIG_JFFS2_LZO)
wdenke84ec902005-05-05 00:04:14 +0000507 "LZO",
wdenke84ec902005-05-05 00:04:14 +0000508#endif
wdenk6b58f332003-03-14 20:47:52 +0000509};
510
wdenk6b58f332003-03-14 20:47:52 +0000511/* Memory management */
512struct mem_block {
513 u32 index;
514 struct mem_block *next;
515 struct b_node nodes[NODE_CHUNK];
516};
517
518
519static void
520free_nodes(struct b_list *list)
521{
522 while (list->listMemBase != NULL) {
523 struct mem_block *next = list->listMemBase->next;
524 free( list->listMemBase );
525 list->listMemBase = next;
526 }
527}
wdenkfe8c2802002-11-03 00:38:21 +0000528
529static struct b_node *
wdenk6b58f332003-03-14 20:47:52 +0000530add_node(struct b_list *list)
wdenkfe8c2802002-11-03 00:38:21 +0000531{
wdenk6b58f332003-03-14 20:47:52 +0000532 u32 index = 0;
533 struct mem_block *memBase;
wdenkfe8c2802002-11-03 00:38:21 +0000534 struct b_node *b;
535
wdenk6b58f332003-03-14 20:47:52 +0000536 memBase = list->listMemBase;
537 if (memBase != NULL)
538 index = memBase->index;
wdenkfe8c2802002-11-03 00:38:21 +0000539#if 0
540 putLabeledWord("add_node: index = ", index);
wdenk6b58f332003-03-14 20:47:52 +0000541 putLabeledWord("add_node: memBase = ", list->listMemBase);
wdenkfe8c2802002-11-03 00:38:21 +0000542#endif
543
wdenk6b58f332003-03-14 20:47:52 +0000544 if (memBase == NULL || index >= NODE_CHUNK) {
545 /* we need more space before we continue */
546 memBase = mmalloc(sizeof(struct mem_block));
547 if (memBase == NULL) {
wdenkfe8c2802002-11-03 00:38:21 +0000548 putstr("add_node: malloc failed\n");
549 return NULL;
550 }
wdenk6b58f332003-03-14 20:47:52 +0000551 memBase->next = list->listMemBase;
552 index = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000553#if 0
554 putLabeledWord("add_node: alloced a new membase at ", *memBase);
555#endif
556
557 }
558 /* now we have room to add it. */
wdenk6b58f332003-03-14 20:47:52 +0000559 b = &memBase->nodes[index];
560 index ++;
wdenkfe8c2802002-11-03 00:38:21 +0000561
wdenk6b58f332003-03-14 20:47:52 +0000562 memBase->index = index;
563 list->listMemBase = memBase;
564 list->listCount++;
565 return b;
566}
wdenkfe8c2802002-11-03 00:38:21 +0000567
wdenk6b58f332003-03-14 20:47:52 +0000568static struct b_node *
Petr Borsodi80c25b12020-05-07 12:25:56 +0200569insert_node(struct b_list *list)
wdenk6b58f332003-03-14 20:47:52 +0000570{
571 struct b_node *new;
wdenkfe8c2802002-11-03 00:38:21 +0000572
wdenk6b58f332003-03-14 20:47:52 +0000573 if (!(new = add_node(list))) {
574 putstr("add_node failed!\r\n");
575 return NULL;
576 }
Mark Tomlinsonefdc2a62015-07-01 16:38:29 +1200577 new->next = NULL;
wdenk6b58f332003-03-14 20:47:52 +0000578
Mark Tomlinsonefdc2a62015-07-01 16:38:29 +1200579 if (list->listTail != NULL)
580 list->listTail->next = new;
wdenk6b58f332003-03-14 20:47:52 +0000581 else
Mark Tomlinsonefdc2a62015-07-01 16:38:29 +1200582 list->listHead = new;
583 list->listTail = new;
wdenkfe8c2802002-11-03 00:38:21 +0000584
wdenk6b58f332003-03-14 20:47:52 +0000585 return new;
wdenkfe8c2802002-11-03 00:38:21 +0000586}
587
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200588#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
wdenk2c9b05d2003-09-10 22:30:53 +0000589/* Sort data entries with the latest version last, so that if there
590 * is overlapping data the latest version will be used.
591 */
wdenk6b58f332003-03-14 20:47:52 +0000592static int compare_inodes(struct b_node *new, struct b_node *old)
593{
Petr Borsodi80c25b12020-05-07 12:25:56 +0200594 return new->version > old->version;
wdenk6b58f332003-03-14 20:47:52 +0000595}
596
wdenk2c9b05d2003-09-10 22:30:53 +0000597/* Sort directory entries so all entries in the same directory
598 * with the same name are grouped together, with the latest version
599 * last. This makes it easy to eliminate all but the latest version
600 * by marking the previous version dead by setting the inode to 0.
601 */
wdenk6b58f332003-03-14 20:47:52 +0000602static int compare_dirents(struct b_node *new, struct b_node *old)
603{
Mark Tomlinson7c6558e2015-07-01 16:38:23 +1200604 /*
605 * Using NULL as the buffer for NOR flash prevents the entire node
606 * being read. This makes most comparisons much quicker as only one
607 * or two entries from the node will be used most of the time.
wdenk2c9b05d2003-09-10 22:30:53 +0000608 */
Mark Tomlinson7c6558e2015-07-01 16:38:23 +1200609 struct jffs2_raw_dirent *jNew = get_node_mem(new->offset, NULL);
610 struct jffs2_raw_dirent *jOld = get_node_mem(old->offset, NULL);
611 int cmp;
612 int ret;
wdenk2c9b05d2003-09-10 22:30:53 +0000613
Mark Tomlinson7c6558e2015-07-01 16:38:23 +1200614 if (jNew->pino != jOld->pino) {
615 /* ascending sort by pino */
616 ret = jNew->pino > jOld->pino;
617 } else if (jNew->nsize != jOld->nsize) {
618 /*
619 * pino is the same, so use ascending sort by nsize,
620 * so we don't do strncmp unless we really must.
wdenk2c9b05d2003-09-10 22:30:53 +0000621 */
Mark Tomlinson7c6558e2015-07-01 16:38:23 +1200622 ret = jNew->nsize > jOld->nsize;
623 } else {
624 /*
625 * length is also the same, so use ascending sort by name
626 */
627 cmp = strncmp((char *)jNew->name, (char *)jOld->name,
628 jNew->nsize);
629 if (cmp != 0) {
630 ret = cmp > 0;
631 } else {
632 /*
633 * we have duplicate names in this directory,
634 * so use ascending sort by version
635 */
636 ret = jNew->version > jOld->version;
637 }
wdenk2c9b05d2003-09-10 22:30:53 +0000638 }
Mark Tomlinson7c6558e2015-07-01 16:38:23 +1200639 put_fl_mem(jNew, NULL);
640 put_fl_mem(jOld, NULL);
wdenk9c53f402003-10-15 23:53:47 +0000641
Mark Tomlinson7c6558e2015-07-01 16:38:23 +1200642 return ret;
wdenk6b58f332003-03-14 20:47:52 +0000643}
644#endif
wdenkfe8c2802002-11-03 00:38:21 +0000645
Wolfgang Denk47f57792005-08-08 01:03:24 +0200646void
647jffs2_free_cache(struct part_info *part)
wdenkfe8c2802002-11-03 00:38:21 +0000648{
wdenk6b58f332003-03-14 20:47:52 +0000649 struct b_lists *pL;
650
651 if (part->jffs2_priv != NULL) {
652 pL = (struct b_lists *)part->jffs2_priv;
653 free_nodes(&pL->frag);
654 free_nodes(&pL->dir);
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300655 free(pL->readbuf);
wdenk6b58f332003-03-14 20:47:52 +0000656 free(pL);
657 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200658}
659
660static u32
661jffs_init_1pass_list(struct part_info *part)
662{
663 struct b_lists *pL;
664
665 jffs2_free_cache(part);
666
wdenk6b58f332003-03-14 20:47:52 +0000667 if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) {
668 pL = (struct b_lists *)part->jffs2_priv;
wdenkfe8c2802002-11-03 00:38:21 +0000669
wdenk6b58f332003-03-14 20:47:52 +0000670 memset(pL, 0, sizeof(*pL));
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200671#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
wdenk6b58f332003-03-14 20:47:52 +0000672 pL->dir.listCompare = compare_dirents;
673 pL->frag.listCompare = compare_inodes;
674#endif
wdenkfe8c2802002-11-03 00:38:21 +0000675 }
676 return 0;
677}
678
679/* find the inode from the slashless name given a parent */
680static long
681jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest)
682{
683 struct b_node *b;
684 struct jffs2_raw_inode *jNode;
wdenk6b58f332003-03-14 20:47:52 +0000685 u32 totalSize = 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000686 u32 latestVersion = 0;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200687 uchar *lDest;
688 uchar *src;
wdenkfe8c2802002-11-03 00:38:21 +0000689 int i;
690 u32 counter = 0;
Petr Borsodi80c25b12020-05-07 12:25:56 +0200691
wdenk2c9b05d2003-09-10 22:30:53 +0000692 /* Find file size before loading any data, so fragments that
693 * start past the end of file can be ignored. A fragment
694 * that is partially in the file is loaded, so extra data may
695 * be loaded up to the next 4K boundary above the file size.
696 * This shouldn't cause trouble when loading kernel images, so
697 * we will live with it.
698 */
Petr Borsodi80c25b12020-05-07 12:25:56 +0200699 int latestOffset = -1;
wdenk2c9b05d2003-09-10 22:30:53 +0000700 for (b = pL->frag.listHead; b != NULL; b = b->next) {
Petr Borsodi80c25b12020-05-07 12:25:56 +0200701 if (inode == b->ino) {
wdenk2c9b05d2003-09-10 22:30:53 +0000702 /* get actual file length from the newest node */
Petr Borsodi80c25b12020-05-07 12:25:56 +0200703 if (b->version >= latestVersion) {
704 latestVersion = b->version;
705 latestOffset = b->offset;
wdenk2c9b05d2003-09-10 22:30:53 +0000706 }
707 }
Petr Borsodi80c25b12020-05-07 12:25:56 +0200708 }
709
710 if (latestOffset >= 0) {
711 jNode = (struct jffs2_raw_inode *)get_fl_mem(latestOffset,
712 sizeof(struct jffs2_raw_inode), pL->readbuf);
713 totalSize = jNode->isize;
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300714 put_fl_mem(jNode, pL->readbuf);
wdenk2c9b05d2003-09-10 22:30:53 +0000715 }
Petr Borsodi80c25b12020-05-07 12:25:56 +0200716
Mark Tomlinson6d998452015-07-01 16:38:22 +1200717 /*
718 * If no destination is provided, we are done.
719 * Just return the total size.
720 */
721 if (!dest)
722 return totalSize;
wdenkfe8c2802002-11-03 00:38:21 +0000723
wdenk6b58f332003-03-14 20:47:52 +0000724 for (b = pL->frag.listHead; b != NULL; b = b->next) {
Petr Borsodi80c25b12020-05-07 12:25:56 +0200725 if (inode == b->ino) {
726 /*
727 * Copy just the node and not the data at this point,
728 * since we don't yet know if we need this data.
729 */
730 jNode = (struct jffs2_raw_inode *)get_fl_mem(b->offset,
731 sizeof(struct jffs2_raw_inode),
732 pL->readbuf);
wdenk6b58f332003-03-14 20:47:52 +0000733#if 0
wdenkfe8c2802002-11-03 00:38:21 +0000734 putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen);
735 putLabeledWord("read_inode: inode = ", jNode->ino);
736 putLabeledWord("read_inode: version = ", jNode->version);
737 putLabeledWord("read_inode: isize = ", jNode->isize);
738 putLabeledWord("read_inode: offset = ", jNode->offset);
739 putLabeledWord("read_inode: csize = ", jNode->csize);
740 putLabeledWord("read_inode: dsize = ", jNode->dsize);
741 putLabeledWord("read_inode: compr = ", jNode->compr);
742 putLabeledWord("read_inode: usercompr = ", jNode->usercompr);
743 putLabeledWord("read_inode: flags = ", jNode->flags);
wdenkfe8c2802002-11-03 00:38:21 +0000744#endif
wdenk2c9b05d2003-09-10 22:30:53 +0000745
wdenkfe8c2802002-11-03 00:38:21 +0000746 if(dest) {
Mark Tomlinsonb71748f2015-07-01 16:38:25 +1200747 /*
748 * Now that the inode has been checked,
749 * read the entire inode, including data.
750 */
751 put_fl_mem(jNode, pL->readbuf);
752 jNode = (struct jffs2_raw_inode *)
753 get_node_mem(b->offset, pL->readbuf);
754 src = ((uchar *)jNode) +
755 sizeof(struct jffs2_raw_inode);
wdenk6b58f332003-03-14 20:47:52 +0000756 /* ignore data behind latest known EOF */
wdenk8886a662004-04-18 19:43:36 +0000757 if (jNode->offset > totalSize) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300758 put_fl_mem(jNode, pL->readbuf);
wdenk6b58f332003-03-14 20:47:52 +0000759 continue;
wdenk8886a662004-04-18 19:43:36 +0000760 }
Ilya Yanokc0c07732008-11-13 19:49:36 +0300761 if (b->datacrc == CRC_UNKNOWN)
762 b->datacrc = data_crc(jNode) ?
763 CRC_OK : CRC_BAD;
764 if (b->datacrc == CRC_BAD) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300765 put_fl_mem(jNode, pL->readbuf);
Ilya Yanokddb02692008-11-13 19:49:33 +0300766 continue;
767 }
wdenk6b58f332003-03-14 20:47:52 +0000768
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200769 lDest = (uchar *) (dest + jNode->offset);
wdenkfe8c2802002-11-03 00:38:21 +0000770#if 0
wdenk6b58f332003-03-14 20:47:52 +0000771 putLabeledWord("read_inode: src = ", src);
wdenkfe8c2802002-11-03 00:38:21 +0000772 putLabeledWord("read_inode: dest = ", lDest);
wdenkfe8c2802002-11-03 00:38:21 +0000773#endif
774 switch (jNode->compr) {
775 case JFFS2_COMPR_NONE:
Wolfgang Denkbf913c12011-10-05 22:58:11 +0200776 ldr_memcpy(lDest, src, jNode->dsize);
wdenkfe8c2802002-11-03 00:38:21 +0000777 break;
778 case JFFS2_COMPR_ZERO:
wdenkfe8c2802002-11-03 00:38:21 +0000779 for (i = 0; i < jNode->dsize; i++)
780 *(lDest++) = 0;
781 break;
782 case JFFS2_COMPR_RTIME:
wdenkfe8c2802002-11-03 00:38:21 +0000783 rtime_decompress(src, lDest, jNode->csize, jNode->dsize);
784 break;
785 case JFFS2_COMPR_DYNRUBIN:
786 /* this is slow but it works */
wdenkfe8c2802002-11-03 00:38:21 +0000787 dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize);
788 break;
789 case JFFS2_COMPR_ZLIB:
Wolfgang Denkbf913c12011-10-05 22:58:11 +0200790 zlib_decompress(src, lDest, jNode->csize, jNode->dsize);
wdenkfe8c2802002-11-03 00:38:21 +0000791 break;
Wolfgang Denk0eded4f2010-01-15 11:10:33 +0100792#if defined(CONFIG_JFFS2_LZO)
wdenke84ec902005-05-05 00:04:14 +0000793 case JFFS2_COMPR_LZO:
Wolfgang Denkbf913c12011-10-05 22:58:11 +0200794 lzo_decompress(src, lDest, jNode->csize, jNode->dsize);
wdenke84ec902005-05-05 00:04:14 +0000795 break;
796#endif
wdenkfe8c2802002-11-03 00:38:21 +0000797 default:
798 /* unknown */
Loïc Minier5d0569a2011-02-03 22:04:26 +0100799 putLabeledWord("UNKNOWN COMPRESSION METHOD = ", jNode->compr);
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300800 put_fl_mem(jNode, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +0000801 return -1;
802 break;
803 }
804 }
805
wdenkfe8c2802002-11-03 00:38:21 +0000806#if 0
wdenkfe8c2802002-11-03 00:38:21 +0000807 putLabeledWord("read_inode: totalSize = ", totalSize);
wdenkfe8c2802002-11-03 00:38:21 +0000808#endif
Petr Borsodi80c25b12020-05-07 12:25:56 +0200809 put_fl_mem(jNode, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +0000810 }
wdenkfe8c2802002-11-03 00:38:21 +0000811 counter++;
812 }
wdenkfe8c2802002-11-03 00:38:21 +0000813
814#if 0
wdenk6b58f332003-03-14 20:47:52 +0000815 putLabeledWord("read_inode: returning = ", totalSize);
wdenkfe8c2802002-11-03 00:38:21 +0000816#endif
wdenk6b58f332003-03-14 20:47:52 +0000817 return totalSize;
wdenkfe8c2802002-11-03 00:38:21 +0000818}
819
820/* find the inode from the slashless name given a parent */
821static u32
822jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino)
823{
824 struct b_node *b;
825 struct jffs2_raw_dirent *jDir;
826 int len;
827 u32 counter;
828 u32 version = 0;
829 u32 inode = 0;
830
831 /* name is assumed slash free */
832 len = strlen(name);
833
834 counter = 0;
835 /* we need to search all and return the inode with the highest version */
wdenk6b58f332003-03-14 20:47:52 +0000836 for(b = pL->dir.listHead; b; b = b->next, counter++) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300837 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
838 pL->readbuf);
wdenk6b58f332003-03-14 20:47:52 +0000839 if ((pino == jDir->pino) && (len == jDir->nsize) &&
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200840 (!strncmp((char *)jDir->name, name, len))) { /* a match */
wdenk8886a662004-04-18 19:43:36 +0000841 if (jDir->version < version) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300842 put_fl_mem(jDir, pL->readbuf);
wdenk2c9b05d2003-09-10 22:30:53 +0000843 continue;
wdenk8886a662004-04-18 19:43:36 +0000844 }
wdenkfe8c2802002-11-03 00:38:21 +0000845
wdenk2c9b05d2003-09-10 22:30:53 +0000846 if (jDir->version == version && inode != 0) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200847 /* I'm pretty sure this isn't legal */
wdenkfe8c2802002-11-03 00:38:21 +0000848 putstr(" ** ERROR ** ");
849 putnstr(jDir->name, jDir->nsize);
850 putLabeledWord(" has dup version =", version);
851 }
852 inode = jDir->ino;
853 version = jDir->version;
854 }
855#if 0
856 putstr("\r\nfind_inode:p&l ->");
857 putnstr(jDir->name, jDir->nsize);
858 putstr("\r\n");
859 putLabeledWord("pino = ", jDir->pino);
860 putLabeledWord("nsize = ", jDir->nsize);
861 putLabeledWord("b = ", (u32) b);
862 putLabeledWord("counter = ", counter);
863#endif
Ilya Yanoke7fd6712008-11-13 19:49:34 +0300864 put_fl_mem(jDir, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +0000865 }
866 return inode;
867}
868
wdenkad276f22004-01-04 16:28:35 +0000869char *mkmodestr(unsigned long mode, char *str)
wdenkfe8c2802002-11-03 00:38:21 +0000870{
wdenk6b58f332003-03-14 20:47:52 +0000871 static const char *l = "xwr";
872 int mask = 1, i;
873 char c;
wdenkfe8c2802002-11-03 00:38:21 +0000874
wdenk6b58f332003-03-14 20:47:52 +0000875 switch (mode & S_IFMT) {
876 case S_IFDIR: str[0] = 'd'; break;
877 case S_IFBLK: str[0] = 'b'; break;
878 case S_IFCHR: str[0] = 'c'; break;
879 case S_IFIFO: str[0] = 'f'; break;
880 case S_IFLNK: str[0] = 'l'; break;
881 case S_IFSOCK: str[0] = 's'; break;
882 case S_IFREG: str[0] = '-'; break;
883 default: str[0] = '?';
884 }
wdenkfe8c2802002-11-03 00:38:21 +0000885
wdenk6b58f332003-03-14 20:47:52 +0000886 for(i = 0; i < 9; i++) {
887 c = l[i%3];
888 str[9-i] = (mode & mask)?c:'-';
889 mask = mask<<1;
890 }
wdenkfe8c2802002-11-03 00:38:21 +0000891
wdenk6b58f332003-03-14 20:47:52 +0000892 if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S';
893 if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S';
894 if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T';
895 str[10] = '\0';
896 return str;
wdenkfe8c2802002-11-03 00:38:21 +0000897}
898
899static inline void dump_stat(struct stat *st, const char *name)
900{
wdenk6b58f332003-03-14 20:47:52 +0000901 char str[20];
902 char s[64], *p;
wdenkfe8c2802002-11-03 00:38:21 +0000903
wdenk6b58f332003-03-14 20:47:52 +0000904 if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */
905 st->st_mtime = 1;
wdenkfe8c2802002-11-03 00:38:21 +0000906
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200907 ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */
wdenkfe8c2802002-11-03 00:38:21 +0000908
wdenk6b58f332003-03-14 20:47:52 +0000909 if ((p = strchr(s,'\n')) != NULL) *p = '\0';
910 if ((p = strchr(s,'\r')) != NULL) *p = '\0';
wdenkfe8c2802002-11-03 00:38:21 +0000911
912/*
wdenk6b58f332003-03-14 20:47:52 +0000913 printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str),
914 st->st_size, s, name);
wdenkfe8c2802002-11-03 00:38:21 +0000915*/
916
wdenk6b58f332003-03-14 20:47:52 +0000917 printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name);
wdenkfe8c2802002-11-03 00:38:21 +0000918}
919
920static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i)
921{
922 char fname[256];
923 struct stat st;
924
925 if(!d || !i) return -1;
926
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200927 strncpy(fname, (char *)d->name, d->nsize);
wdenk6b58f332003-03-14 20:47:52 +0000928 fname[d->nsize] = '\0';
wdenkfe8c2802002-11-03 00:38:21 +0000929
930 memset(&st,0,sizeof(st));
931
wdenk6b58f332003-03-14 20:47:52 +0000932 st.st_mtime = i->mtime;
933 st.st_mode = i->mode;
934 st.st_ino = i->ino;
Ilya Yanokc4785fb2008-11-13 19:49:31 +0300935 st.st_size = i->isize;
wdenkfe8c2802002-11-03 00:38:21 +0000936
937 dump_stat(&st, fname);
938
939 if (d->type == DT_LNK) {
940 unsigned char *src = (unsigned char *) (&i[1]);
941 putstr(" -> ");
942 putnstr(src, (int)i->dsize);
943 }
944
945 putstr("\r\n");
946
947 return 0;
948}
949
950/* list inodes with the given pino */
951static u32
952jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino)
953{
954 struct b_node *b;
955 struct jffs2_raw_dirent *jDir;
956
wdenk6b58f332003-03-14 20:47:52 +0000957 for (b = pL->dir.listHead; b; b = b->next) {
Petr Borsodi80c25b12020-05-07 12:25:56 +0200958 if (pino == b->pino) {
wdenk6b58f332003-03-14 20:47:52 +0000959 u32 i_version = 0;
Petr Borsodi80c25b12020-05-07 12:25:56 +0200960 int i_offset = -1;
961 struct jffs2_raw_inode *jNode = NULL;
Mark Tomlinson29468522015-07-01 16:38:24 +1200962 struct b_node *b2;
wdenkfe8c2802002-11-03 00:38:21 +0000963
Petr Borsodi80c25b12020-05-07 12:25:56 +0200964 jDir = (struct jffs2_raw_dirent *)
965 get_node_mem(b->offset, pL->readbuf);
Mark Tomlinson29468522015-07-01 16:38:24 +1200966#ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS
967 /* Check for more recent versions of this file */
968 int match;
969 do {
970 struct b_node *next = b->next;
971 struct jffs2_raw_dirent *jDirNext;
972 if (!next)
973 break;
974 jDirNext = (struct jffs2_raw_dirent *)
975 get_node_mem(next->offset, NULL);
976 match = jDirNext->pino == jDir->pino &&
977 jDirNext->nsize == jDir->nsize &&
978 strncmp((char *)jDirNext->name,
979 (char *)jDir->name,
980 jDir->nsize) == 0;
981 if (match) {
982 /* Use next. It is more recent */
983 b = next;
984 /* Update buffer with the new info */
985 *jDir = *jDirNext;
986 }
987 put_fl_mem(jDirNext, NULL);
988 } while (match);
989#endif
990 if (jDir->ino == 0) {
991 /* Deleted file */
992 put_fl_mem(jDir, pL->readbuf);
993 continue;
994 }
995
996 for (b2 = pL->frag.listHead; b2; b2 = b2->next) {
Petr Borsodi80c25b12020-05-07 12:25:56 +0200997 if (b2->ino == jDir->ino &&
998 b2->version >= i_version) {
999 i_version = b2->version;
1000 i_offset = b2->offset;
wdenkf248ebc2004-05-05 19:44:41 +00001001 }
wdenkfe8c2802002-11-03 00:38:21 +00001002 }
1003
Petr Borsodi80c25b12020-05-07 12:25:56 +02001004 if (i_version >= 0) {
1005 if (jDir->type == DT_LNK)
1006 jNode = get_node_mem(i_offset, NULL);
1007 else
1008 jNode = get_fl_mem(i_offset,
1009 sizeof(*jNode),
1010 NULL);
1011 }
1012
1013 dump_inode(pL, jDir, jNode);
1014 put_fl_mem(jNode, NULL);
1015
1016 put_fl_mem(jDir, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001017 }
1018 }
1019 return pino;
1020}
1021
1022static u32
1023jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino)
1024{
1025 int i;
1026 char tmp[256];
1027 char working_tmp[256];
1028 char *c;
1029
1030 /* discard any leading slash */
1031 i = 0;
1032 while (fname[i] == '/')
1033 i++;
1034 strcpy(tmp, &fname[i]);
1035
1036 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
1037 {
1038 strncpy(working_tmp, tmp, c - tmp);
1039 working_tmp[c - tmp] = '\0';
1040#if 0
1041 putstr("search_inode: tmp = ");
1042 putstr(tmp);
1043 putstr("\r\n");
1044 putstr("search_inode: wtmp = ");
1045 putstr(working_tmp);
1046 putstr("\r\n");
1047 putstr("search_inode: c = ");
1048 putstr(c);
1049 putstr("\r\n");
1050#endif
1051 for (i = 0; i < strlen(c) - 1; i++)
1052 tmp[i] = c[i + 1];
1053 tmp[i] = '\0';
1054#if 0
1055 putstr("search_inode: post tmp = ");
1056 putstr(tmp);
1057 putstr("\r\n");
1058#endif
1059
1060 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) {
1061 putstr("find_inode failed for name=");
1062 putstr(working_tmp);
1063 putstr("\r\n");
1064 return 0;
1065 }
1066 }
1067 /* this is for the bare filename, directories have already been mapped */
1068 if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1069 putstr("find_inode failed for name=");
1070 putstr(tmp);
1071 putstr("\r\n");
1072 return 0;
1073 }
1074 return pino;
1075
1076}
1077
1078static u32
1079jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino)
1080{
1081 struct b_node *b;
1082 struct b_node *b2;
1083 struct jffs2_raw_dirent *jDir;
1084 struct jffs2_raw_inode *jNode;
wdenk8886a662004-04-18 19:43:36 +00001085 u8 jDirFoundType = 0;
1086 u32 jDirFoundIno = 0;
1087 u32 jDirFoundPino = 0;
wdenkfe8c2802002-11-03 00:38:21 +00001088 char tmp[256];
1089 u32 version = 0;
1090 u32 pino;
1091 unsigned char *src;
1092
1093 /* we need to search all and return the inode with the highest version */
wdenk6b58f332003-03-14 20:47:52 +00001094 for(b = pL->dir.listHead; b; b = b->next) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001095 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
1096 pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001097 if (ino == jDir->ino) {
Wolfgang Denka1be4762008-05-20 16:00:29 +02001098 if (jDir->version < version) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001099 put_fl_mem(jDir, pL->readbuf);
wdenk2c9b05d2003-09-10 22:30:53 +00001100 continue;
wdenk8886a662004-04-18 19:43:36 +00001101 }
wdenkfe8c2802002-11-03 00:38:21 +00001102
wdenk8886a662004-04-18 19:43:36 +00001103 if (jDir->version == version && jDirFoundType) {
Wolfgang Denka1be4762008-05-20 16:00:29 +02001104 /* I'm pretty sure this isn't legal */
wdenkfe8c2802002-11-03 00:38:21 +00001105 putstr(" ** ERROR ** ");
1106 putnstr(jDir->name, jDir->nsize);
1107 putLabeledWord(" has dup version (resolve) = ",
1108 version);
1109 }
1110
wdenk8886a662004-04-18 19:43:36 +00001111 jDirFoundType = jDir->type;
1112 jDirFoundIno = jDir->ino;
1113 jDirFoundPino = jDir->pino;
wdenkfe8c2802002-11-03 00:38:21 +00001114 version = jDir->version;
1115 }
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001116 put_fl_mem(jDir, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001117 }
1118 /* now we found the right entry again. (shoulda returned inode*) */
wdenk8886a662004-04-18 19:43:36 +00001119 if (jDirFoundType != DT_LNK)
1120 return jDirFoundIno;
wdenk6b58f332003-03-14 20:47:52 +00001121
1122 /* it's a soft link so we follow it again. */
1123 b2 = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +00001124 while (b2) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001125 jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset,
1126 pL->readbuf);
wdenk8886a662004-04-18 19:43:36 +00001127 if (jNode->ino == jDirFoundIno) {
wdenk6c59edc2004-05-03 20:45:30 +00001128 src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode);
wdenkfe8c2802002-11-03 00:38:21 +00001129
1130#if 0
1131 putLabeledWord("\t\t dsize = ", jNode->dsize);
1132 putstr("\t\t target = ");
1133 putnstr(src, jNode->dsize);
1134 putstr("\r\n");
1135#endif
Wolfgang Denk7fb52662005-10-13 16:45:02 +02001136 strncpy(tmp, (char *)src, jNode->dsize);
wdenkfe8c2802002-11-03 00:38:21 +00001137 tmp[jNode->dsize] = '\0';
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001138 put_fl_mem(jNode, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001139 break;
1140 }
1141 b2 = b2->next;
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001142 put_fl_mem(jNode, pL->readbuf);
wdenkfe8c2802002-11-03 00:38:21 +00001143 }
1144 /* ok so the name of the new file to find is in tmp */
1145 /* if it starts with a slash it is root based else shared dirs */
1146 if (tmp[0] == '/')
1147 pino = 1;
1148 else
wdenk8886a662004-04-18 19:43:36 +00001149 pino = jDirFoundPino;
wdenkfe8c2802002-11-03 00:38:21 +00001150
1151 return jffs2_1pass_search_inode(pL, tmp, pino);
1152}
1153
1154static u32
1155jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino)
1156{
1157 int i;
1158 char tmp[256];
1159 char working_tmp[256];
1160 char *c;
1161
1162 /* discard any leading slash */
1163 i = 0;
1164 while (fname[i] == '/')
1165 i++;
1166 strcpy(tmp, &fname[i]);
1167 working_tmp[0] = '\0';
1168 while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */
1169 {
1170 strncpy(working_tmp, tmp, c - tmp);
1171 working_tmp[c - tmp] = '\0';
1172 for (i = 0; i < strlen(c) - 1; i++)
1173 tmp[i] = c[i + 1];
1174 tmp[i] = '\0';
1175 /* only a failure if we arent looking at top level */
wdenk6b58f332003-03-14 20:47:52 +00001176 if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) &&
1177 (working_tmp[0])) {
wdenkfe8c2802002-11-03 00:38:21 +00001178 putstr("find_inode failed for name=");
1179 putstr(working_tmp);
1180 putstr("\r\n");
1181 return 0;
1182 }
1183 }
1184
1185 if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) {
1186 putstr("find_inode failed for name=");
1187 putstr(tmp);
1188 putstr("\r\n");
1189 return 0;
1190 }
1191 /* this is for the bare filename, directories have already been mapped */
1192 if (!(pino = jffs2_1pass_list_inodes(pL, pino))) {
1193 putstr("find_inode failed for name=");
1194 putstr(tmp);
1195 putstr("\r\n");
1196 return 0;
1197 }
1198 return pino;
1199
1200}
1201
1202unsigned char
1203jffs2_1pass_rescan_needed(struct part_info *part)
1204{
1205 struct b_node *b;
wdenk8886a662004-04-18 19:43:36 +00001206 struct jffs2_unknown_node onode;
wdenkfe8c2802002-11-03 00:38:21 +00001207 struct jffs2_unknown_node *node;
wdenk6b58f332003-03-14 20:47:52 +00001208 struct b_lists *pL = (struct b_lists *)part->jffs2_priv;
wdenkfe8c2802002-11-03 00:38:21 +00001209
1210 if (part->jffs2_priv == 0){
1211 DEBUGF ("rescan: First time in use\n");
1212 return 1;
1213 }
Wolfgang Denk47f57792005-08-08 01:03:24 +02001214
wdenkfe8c2802002-11-03 00:38:21 +00001215 /* if we have no list, we need to rescan */
wdenk6b58f332003-03-14 20:47:52 +00001216 if (pL->frag.listCount == 0) {
wdenkfe8c2802002-11-03 00:38:21 +00001217 DEBUGF ("rescan: fraglist zero\n");
1218 return 1;
1219 }
1220
wdenk6b58f332003-03-14 20:47:52 +00001221 /* but suppose someone reflashed a partition at the same offset... */
1222 b = pL->dir.listHead;
wdenkfe8c2802002-11-03 00:38:21 +00001223 while (b) {
wdenk8886a662004-04-18 19:43:36 +00001224 node = (struct jffs2_unknown_node *) get_fl_mem(b->offset,
1225 sizeof(onode), &onode);
wdenkfe8c2802002-11-03 00:38:21 +00001226 if (node->nodetype != JFFS2_NODETYPE_DIRENT) {
wdenk6b58f332003-03-14 20:47:52 +00001227 DEBUGF ("rescan: fs changed beneath me? (%lx)\n",
1228 (unsigned long) b->offset);
wdenkfe8c2802002-11-03 00:38:21 +00001229 return 1;
1230 }
1231 b = b->next;
1232 }
1233 return 0;
1234}
wdenk6b58f332003-03-14 20:47:52 +00001235
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001236#ifdef CONFIG_JFFS2_SUMMARY
1237static u32 sum_get_unaligned32(u32 *ptr)
1238{
1239 u32 val;
1240 u8 *p = (u8 *)ptr;
1241
1242 val = *p | (*(p + 1) << 8) | (*(p + 2) << 16) | (*(p + 3) << 24);
1243
1244 return __le32_to_cpu(val);
1245}
1246
1247static u16 sum_get_unaligned16(u16 *ptr)
1248{
1249 u16 val;
1250 u8 *p = (u8 *)ptr;
1251
1252 val = *p | (*(p + 1) << 8);
1253
1254 return __le16_to_cpu(val);
1255}
1256
Ilya Yanoka933bd62008-11-13 19:49:35 +03001257#define dbg_summary(...) do {} while (0);
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001258/*
1259 * Process the stored summary information - helper function for
Ilya Yanoka933bd62008-11-13 19:49:35 +03001260 * jffs2_sum_scan_sumnode()
1261 */
1262
1263static int jffs2_sum_process_sum_data(struct part_info *part, uint32_t offset,
1264 struct jffs2_raw_summary *summary,
1265 struct b_lists *pL)
1266{
1267 void *sp;
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001268 int i, pass;
Petr Borsodi80c25b12020-05-07 12:25:56 +02001269 struct b_node *b;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001270
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001271 for (pass = 0; pass < 2; pass++) {
1272 sp = summary->sum;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001273
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001274 for (i = 0; i < summary->sum_num; i++) {
1275 struct jffs2_sum_unknown_flash *spu = sp;
1276 dbg_summary("processing summary index %d\n", i);
Ilya Yanoka933bd62008-11-13 19:49:35 +03001277
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001278 switch (sum_get_unaligned16(&spu->nodetype)) {
1279 case JFFS2_NODETYPE_INODE: {
Ilya Yanoka933bd62008-11-13 19:49:35 +03001280 struct jffs2_sum_inode_flash *spi;
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001281 if (pass) {
1282 spi = sp;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001283
Petr Borsodi80c25b12020-05-07 12:25:56 +02001284 b = insert_node(&pL->frag);
1285 if (!b)
1286 return -1;
1287 b->offset = (u32)part->offset +
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001288 offset +
1289 sum_get_unaligned32(
Petr Borsodi80c25b12020-05-07 12:25:56 +02001290 &spi->offset);
1291 b->version = sum_get_unaligned32(
1292 &spi->version);
1293 b->ino = sum_get_unaligned32(
1294 &spi->inode);
Wagner Popov dos Santosb9e3b672021-02-23 00:49:00 -03001295 b->datacrc = CRC_UNKNOWN;
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001296 }
Ilya Yanoka933bd62008-11-13 19:49:35 +03001297
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001298 sp += JFFS2_SUMMARY_INODE_SIZE;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001299
Ilya Yanokbcaa1f72009-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 Borsodi80c25b12020-05-07 12:25:56 +02001306 b = insert_node(&pL->dir);
1307 if (!b)
1308 return -1;
1309 b->offset = (u32)part->offset +
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001310 offset +
1311 sum_get_unaligned32(
Petr Borsodi80c25b12020-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);
Wagner Popov dos Santosb9e3b672021-02-23 00:49:00 -03001317 b->datacrc = CRC_UNKNOWN;
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001318 }
Ilya Yanoka933bd62008-11-13 19:49:35 +03001319
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001320 sp += JFFS2_SUMMARY_DIRENT_SIZE(
1321 spd->nsize);
Ilya Yanoka933bd62008-11-13 19:49:35 +03001322
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001323 break;
1324 }
1325 default : {
1326 uint16_t nodetype = sum_get_unaligned16(
1327 &spu->nodetype);
1328 printf("Unsupported node type %x found"
1329 " in summary!\n",
1330 nodetype);
1331 if ((nodetype & JFFS2_COMPAT_MASK) ==
1332 JFFS2_FEATURE_INCOMPAT)
1333 return -EIO;
1334 return -EBADMSG;
1335 }
Ilya Yanoka933bd62008-11-13 19:49:35 +03001336 }
1337 }
1338 }
1339 return 0;
1340}
1341
1342/* Process the summary node - called from jffs2_scan_eraseblock() */
1343int jffs2_sum_scan_sumnode(struct part_info *part, uint32_t offset,
1344 struct jffs2_raw_summary *summary, uint32_t sumsize,
1345 struct b_lists *pL)
1346{
1347 struct jffs2_unknown_node crcnode;
Tom Rini7c4734d2016-03-27 14:48:36 -04001348 int ret, __maybe_unused ofs;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001349 uint32_t crc;
1350
1351 ofs = part->sector_size - sumsize;
1352
1353 dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n",
1354 offset, offset + ofs, sumsize);
1355
1356 /* OK, now check for node validity and CRC */
1357 crcnode.magic = JFFS2_MAGIC_BITMASK;
1358 crcnode.nodetype = JFFS2_NODETYPE_SUMMARY;
1359 crcnode.totlen = summary->totlen;
1360 crc = crc32_no_comp(0, (uchar *)&crcnode, sizeof(crcnode)-4);
1361
1362 if (summary->hdr_crc != crc) {
1363 dbg_summary("Summary node header is corrupt (bad CRC or "
1364 "no summary at all)\n");
1365 goto crc_err;
1366 }
1367
1368 if (summary->totlen != sumsize) {
1369 dbg_summary("Summary node is corrupt (wrong erasesize?)\n");
1370 goto crc_err;
1371 }
1372
1373 crc = crc32_no_comp(0, (uchar *)summary,
1374 sizeof(struct jffs2_raw_summary)-8);
1375
1376 if (summary->node_crc != crc) {
1377 dbg_summary("Summary node is corrupt (bad CRC)\n");
1378 goto crc_err;
1379 }
1380
1381 crc = crc32_no_comp(0, (uchar *)summary->sum,
1382 sumsize - sizeof(struct jffs2_raw_summary));
1383
1384 if (summary->sum_crc != crc) {
1385 dbg_summary("Summary node data is corrupt (bad CRC)\n");
1386 goto crc_err;
1387 }
1388
1389 if (summary->cln_mkr)
1390 dbg_summary("Summary : CLEANMARKER node \n");
1391
1392 ret = jffs2_sum_process_sum_data(part, offset, summary, pL);
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001393 if (ret == -EBADMSG)
1394 return 0;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001395 if (ret)
1396 return ret; /* real error */
1397
1398 return 1;
1399
1400crc_err:
1401 putstr("Summary node crc error, skipping summary information.\n");
1402
1403 return 0;
1404}
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001405#endif /* CONFIG_JFFS2_SUMMARY */
Ilya Yanoka933bd62008-11-13 19:49:35 +03001406
wdenk6b58f332003-03-14 20:47:52 +00001407#ifdef DEBUG_FRAGMENTS
1408static void
1409dump_fragments(struct b_lists *pL)
1410{
1411 struct b_node *b;
wdenk8886a662004-04-18 19:43:36 +00001412 struct jffs2_raw_inode ojNode;
wdenk6b58f332003-03-14 20:47:52 +00001413 struct jffs2_raw_inode *jNode;
1414
1415 putstr("\r\n\r\n******The fragment Entries******\r\n");
1416 b = pL->frag.listHead;
1417 while (b) {
wdenk8886a662004-04-18 19:43:36 +00001418 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1419 sizeof(ojNode), &ojNode);
wdenk6b58f332003-03-14 20:47:52 +00001420 putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset);
1421 putLabeledWord("\tbuild_list: totlen = ", jNode->totlen);
1422 putLabeledWord("\tbuild_list: inode = ", jNode->ino);
1423 putLabeledWord("\tbuild_list: version = ", jNode->version);
1424 putLabeledWord("\tbuild_list: isize = ", jNode->isize);
1425 putLabeledWord("\tbuild_list: atime = ", jNode->atime);
1426 putLabeledWord("\tbuild_list: offset = ", jNode->offset);
1427 putLabeledWord("\tbuild_list: csize = ", jNode->csize);
1428 putLabeledWord("\tbuild_list: dsize = ", jNode->dsize);
1429 putLabeledWord("\tbuild_list: compr = ", jNode->compr);
1430 putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr);
1431 putLabeledWord("\tbuild_list: flags = ", jNode->flags);
wdenk57b2d802003-06-27 21:31:46 +00001432 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
wdenk6b58f332003-03-14 20:47:52 +00001433 b = b->next;
1434 }
1435}
1436#endif
1437
1438#ifdef DEBUG_DIRENTS
1439static void
1440dump_dirents(struct b_lists *pL)
1441{
1442 struct b_node *b;
1443 struct jffs2_raw_dirent *jDir;
1444
1445 putstr("\r\n\r\n******The directory Entries******\r\n");
1446 b = pL->dir.listHead;
1447 while (b) {
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001448 jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset,
1449 pL->readbuf);
wdenk6b58f332003-03-14 20:47:52 +00001450 putstr("\r\n");
1451 putnstr(jDir->name, jDir->nsize);
1452 putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic);
1453 putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype);
1454 putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc);
1455 putLabeledWord("\tbuild_list: pino = ", jDir->pino);
1456 putLabeledWord("\tbuild_list: version = ", jDir->version);
1457 putLabeledWord("\tbuild_list: ino = ", jDir->ino);
1458 putLabeledWord("\tbuild_list: mctime = ", jDir->mctime);
1459 putLabeledWord("\tbuild_list: nsize = ", jDir->nsize);
1460 putLabeledWord("\tbuild_list: type = ", jDir->type);
1461 putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc);
1462 putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc);
Wolfgang Denka1be4762008-05-20 16:00:29 +02001463 putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */
wdenk6b58f332003-03-14 20:47:52 +00001464 b = b->next;
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001465 put_fl_mem(jDir, pL->readbuf);
wdenk6b58f332003-03-14 20:47:52 +00001466 }
1467}
1468#endif
wdenkfe8c2802002-11-03 00:38:21 +00001469
Mark Tomlinsonf89bfd02015-07-01 16:38:27 +12001470#define DEFAULT_EMPTY_SCAN_SIZE 256
Ilya Yanokddb02692008-11-13 19:49:33 +03001471
1472static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size)
1473{
1474 if (sector_size < DEFAULT_EMPTY_SCAN_SIZE)
1475 return sector_size;
1476 else
1477 return DEFAULT_EMPTY_SCAN_SIZE;
1478}
1479
wdenkfe8c2802002-11-03 00:38:21 +00001480static u32
1481jffs2_1pass_build_lists(struct part_info * part)
1482{
1483 struct b_lists *pL;
Petr Borsodia61d1d12020-05-07 12:25:55 +02001484 union jffs2_node_union *node;
Tom Rini3cd84d62013-12-05 14:48:38 -05001485 u32 nr_sectors;
Ilya Yanokddb02692008-11-13 19:49:33 +03001486 u32 i;
wdenkfe8c2802002-11-03 00:38:21 +00001487 u32 counter4 = 0;
1488 u32 counterF = 0;
1489 u32 counterN = 0;
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001490 u32 max_totlen = 0;
Mark Tomlinsonef619822015-07-01 16:38:26 +12001491 u32 buf_size;
Ilya Yanokddb02692008-11-13 19:49:33 +03001492 char *buf;
wdenkfe8c2802002-11-03 00:38:21 +00001493
Tom Rini3cd84d62013-12-05 14:48:38 -05001494 nr_sectors = lldiv(part->size, part->sector_size);
wdenkfe8c2802002-11-03 00:38:21 +00001495 /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */
1496 /* jffs2 list building enterprise nope. in newer versions the overhead is */
1497 /* only about 5 %. not enough to inconvenience people for. */
1498 /* lcd_off(); */
1499
1500 /* if we are building a list we need to refresh the cache. */
wdenkfe8c2802002-11-03 00:38:21 +00001501 jffs_init_1pass_list(part);
wdenk6b58f332003-03-14 20:47:52 +00001502 pL = (struct b_lists *)part->jffs2_priv;
Mark Tomlinsonef619822015-07-01 16:38:26 +12001503 buf = malloc(DEFAULT_EMPTY_SCAN_SIZE);
wdenk42c05472004-03-23 22:14:11 +00001504 puts ("Scanning JFFS2 FS: ");
wdenkfe8c2802002-11-03 00:38:21 +00001505
1506 /* start at the beginning of the partition */
Ilya Yanokddb02692008-11-13 19:49:33 +03001507 for (i = 0; i < nr_sectors; i++) {
1508 uint32_t sector_ofs = i * part->sector_size;
1509 uint32_t buf_ofs = sector_ofs;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001510 uint32_t buf_len;
Ilya Yanokddb02692008-11-13 19:49:33 +03001511 uint32_t ofs, prevofs;
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001512#ifdef CONFIG_JFFS2_SUMMARY
Ilya Yanoka933bd62008-11-13 19:49:35 +03001513 struct jffs2_sum_marker *sm;
1514 void *sumptr = NULL;
1515 uint32_t sumlen;
1516 int ret;
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001517#endif
Mark Tomlinson56a7f482015-07-01 16:38:28 +12001518 /* Indicates a sector with a CLEANMARKER was found */
1519 int clean_sector = 0;
Petr Borsodia61d1d12020-05-07 12:25:55 +02001520 struct jffs2_unknown_node crcnode;
Petr Borsodi80c25b12020-05-07 12:25:56 +02001521 struct b_node *b;
wdenk7ad5e4c2004-04-25 15:41:35 +00001522
Mark Tomlinsonef619822015-07-01 16:38:26 +12001523 /* Set buf_size to maximum length */
1524 buf_size = DEFAULT_EMPTY_SCAN_SIZE;
Stefan Roese80877fa2022-09-02 14:10:46 +02001525 schedule();
Ilya Yanoka933bd62008-11-13 19:49:35 +03001526
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001527#ifdef CONFIG_JFFS2_SUMMARY
Ilya Yanoka933bd62008-11-13 19:49:35 +03001528 buf_len = sizeof(*sm);
1529
1530 /* Read as much as we want into the _end_ of the preallocated
1531 * buffer
1532 */
1533 get_fl_mem(part->offset + sector_ofs + part->sector_size -
1534 buf_len, buf_len, buf + buf_size - buf_len);
1535
1536 sm = (void *)buf + buf_size - sizeof(*sm);
1537 if (sm->magic == JFFS2_SUM_MAGIC) {
1538 sumlen = part->sector_size - sm->offset;
1539 sumptr = buf + buf_size - sumlen;
1540
1541 /* Now, make sure the summary itself is available */
1542 if (sumlen > buf_size) {
1543 /* Need to kmalloc for this. */
1544 sumptr = malloc(sumlen);
1545 if (!sumptr) {
1546 putstr("Can't get memory for summary "
1547 "node!\n");
Ilya Yanoka87011e2009-08-12 16:42:48 +04001548 free(buf);
1549 jffs2_free_cache(part);
Ilya Yanoka933bd62008-11-13 19:49:35 +03001550 return 0;
1551 }
1552 memcpy(sumptr + sumlen - buf_len, buf +
1553 buf_size - buf_len, buf_len);
1554 }
1555 if (buf_len < sumlen) {
1556 /* Need to read more so that the entire summary
1557 * node is present
1558 */
1559 get_fl_mem(part->offset + sector_ofs +
1560 part->sector_size - sumlen,
1561 sumlen - buf_len, sumptr);
1562 }
1563 }
1564
1565 if (sumptr) {
1566 ret = jffs2_sum_scan_sumnode(part, sector_ofs, sumptr,
1567 sumlen, pL);
1568
1569 if (buf_size && sumlen > buf_size)
1570 free(sumptr);
Ilya Yanoka87011e2009-08-12 16:42:48 +04001571 if (ret < 0) {
1572 free(buf);
1573 jffs2_free_cache(part);
Ilya Yanoka933bd62008-11-13 19:49:35 +03001574 return 0;
Ilya Yanoka87011e2009-08-12 16:42:48 +04001575 }
Ilya Yanoka933bd62008-11-13 19:49:35 +03001576 if (ret)
1577 continue;
1578
1579 }
Ilya Yanokbcaa1f72009-07-17 15:02:42 +04001580#endif /* CONFIG_JFFS2_SUMMARY */
Ilya Yanoka933bd62008-11-13 19:49:35 +03001581
1582 buf_len = EMPTY_SCAN_SIZE(part->sector_size);
1583
Ilya Yanokddb02692008-11-13 19:49:33 +03001584 get_fl_mem((u32)part->offset + buf_ofs, buf_len, buf);
Stuart Wood471a2f02008-06-02 16:40:08 -04001585
Ilya Yanokddb02692008-11-13 19:49:33 +03001586 /* We temporarily use 'ofs' as a pointer into the buffer/jeb */
1587 ofs = 0;
1588
1589 /* Scan only 4KiB of 0xFF before declaring it's empty */
1590 while (ofs < EMPTY_SCAN_SIZE(part->sector_size) &&
1591 *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF)
1592 ofs += 4;
1593
1594 if (ofs == EMPTY_SCAN_SIZE(part->sector_size))
1595 continue;
1596
1597 ofs += sector_ofs;
1598 prevofs = ofs - 1;
Mark Tomlinsonef619822015-07-01 16:38:26 +12001599 /*
1600 * Set buf_size down to the minimum size required.
1601 * This prevents reading in chunks of flash data unnecessarily.
1602 */
1603 buf_size = sizeof(union jffs2_node_union);
Ilya Yanokddb02692008-11-13 19:49:33 +03001604
1605 scan_more:
1606 while (ofs < sector_ofs + part->sector_size) {
1607 if (ofs == prevofs) {
1608 printf("offset %08x already seen, skip\n", ofs);
1609 ofs += 4;
1610 counter4++;
1611 continue;
1612 }
1613 prevofs = ofs;
1614 if (sector_ofs + part->sector_size <
Petr Borsodia61d1d12020-05-07 12:25:55 +02001615 ofs + sizeof(struct jffs2_unknown_node))
Ilya Yanokddb02692008-11-13 19:49:33 +03001616 break;
Petr Borsodia61d1d12020-05-07 12:25:55 +02001617 if (buf_ofs + buf_len <
1618 ofs + sizeof(struct jffs2_unknown_node)) {
Ilya Yanokddb02692008-11-13 19:49:33 +03001619 buf_len = min_t(uint32_t, buf_size, sector_ofs
1620 + part->sector_size - ofs);
1621 get_fl_mem((u32)part->offset + ofs, buf_len,
1622 buf);
1623 buf_ofs = ofs;
1624 }
1625
Petr Borsodia61d1d12020-05-07 12:25:55 +02001626 node = (union jffs2_node_union *)&buf[ofs - buf_ofs];
Ilya Yanokddb02692008-11-13 19:49:33 +03001627
1628 if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) {
1629 uint32_t inbuf_ofs;
Wolfgang Denkbf913c12011-10-05 22:58:11 +02001630 uint32_t scan_end;
Ilya Yanokddb02692008-11-13 19:49:33 +03001631
Ilya Yanokddb02692008-11-13 19:49:33 +03001632 ofs += 4;
1633 scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE(
1634 part->sector_size)/8,
1635 buf_len);
1636 more_empty:
1637 inbuf_ofs = ofs - buf_ofs;
1638 while (inbuf_ofs < scan_end) {
1639 if (*(uint32_t *)(&buf[inbuf_ofs]) !=
1640 0xffffffff)
1641 goto scan_more;
1642
1643 inbuf_ofs += 4;
1644 ofs += 4;
1645 }
1646 /* Ran off end. */
Mark Tomlinson56a7f482015-07-01 16:38:28 +12001647 /*
1648 * If this sector had a clean marker at the
1649 * beginning, and immediately following this
1650 * have been a bunch of FF bytes, treat the
1651 * entire sector as empty.
1652 */
1653 if (clean_sector)
1654 break;
Ilya Yanokddb02692008-11-13 19:49:33 +03001655
1656 /* See how much more there is to read in this
1657 * eraseblock...
1658 */
1659 buf_len = min_t(uint32_t, buf_size,
1660 sector_ofs +
1661 part->sector_size - ofs);
1662 if (!buf_len) {
1663 /* No more to read. Break out of main
1664 * loop without marking this range of
1665 * empty space as dirty (because it's
1666 * not)
1667 */
1668 break;
1669 }
1670 scan_end = buf_len;
1671 get_fl_mem((u32)part->offset + ofs, buf_len,
1672 buf);
1673 buf_ofs = ofs;
1674 goto more_empty;
1675 }
Mark Tomlinson56a7f482015-07-01 16:38:28 +12001676 /*
1677 * Found something not erased in the sector, so reset
1678 * the 'clean_sector' flag.
1679 */
1680 clean_sector = 0;
Petr Borsodia61d1d12020-05-07 12:25:55 +02001681 if (node->u.magic != JFFS2_MAGIC_BITMASK) {
Ilya Yanokddb02692008-11-13 19:49:33 +03001682 ofs += 4;
1683 counter4++;
1684 continue;
1685 }
Petr Borsodia61d1d12020-05-07 12:25:55 +02001686
1687 crcnode.magic = node->u.magic;
1688 crcnode.nodetype = node->u.nodetype | JFFS2_NODE_ACCURATE;
1689 crcnode.totlen = node->u.totlen;
1690 crcnode.hdr_crc = node->u.hdr_crc;
1691 if (!hdr_crc(&crcnode)) {
1692 ofs += 4;
1693 counter4++;
1694 continue;
1695 }
1696
1697 if (ofs + node->u.totlen > sector_ofs + part->sector_size) {
Ilya Yanokddb02692008-11-13 19:49:33 +03001698 ofs += 4;
1699 counter4++;
1700 continue;
1701 }
Petr Borsodia61d1d12020-05-07 12:25:55 +02001702
1703 if (!(node->u.nodetype & JFFS2_NODE_ACCURATE)) {
1704 DEBUGF("Obsolete node type: %x len %d offset 0x%x\n",
1705 node->u.nodetype, node->u.totlen, ofs);
1706 ofs += ((node->u.totlen + 3) & ~3);
1707 counterF++;
1708 continue;
1709 }
1710
wdenkfe8c2802002-11-03 00:38:21 +00001711 /* if its a fragment add it */
Petr Borsodia61d1d12020-05-07 12:25:55 +02001712 switch (node->u.nodetype) {
Ilya Yanokddb02692008-11-13 19:49:33 +03001713 case JFFS2_NODETYPE_INODE:
Petr Borsodia61d1d12020-05-07 12:25:55 +02001714 if (buf_ofs + buf_len <
1715 ofs + sizeof(struct jffs2_raw_inode)) {
Mark Tomlinsonef619822015-07-01 16:38:26 +12001716 buf_len = min_t(uint32_t,
1717 sizeof(struct jffs2_raw_inode),
1718 sector_ofs +
1719 part->sector_size -
1720 ofs);
Ilya Yanokddb02692008-11-13 19:49:33 +03001721 get_fl_mem((u32)part->offset + ofs,
1722 buf_len, buf);
1723 buf_ofs = ofs;
1724 node = (void *)buf;
1725 }
Mark Tomlinsonef619822015-07-01 16:38:26 +12001726 if (!inode_crc((struct jffs2_raw_inode *)node))
1727 break;
Ilya Yanokddb02692008-11-13 19:49:33 +03001728
Petr Borsodi80c25b12020-05-07 12:25:56 +02001729 b = insert_node(&pL->frag);
1730 if (!b) {
Ilya Yanoka87011e2009-08-12 16:42:48 +04001731 free(buf);
1732 jffs2_free_cache(part);
wdenkfe8c2802002-11-03 00:38:21 +00001733 return 0;
Ilya Yanoka87011e2009-08-12 16:42:48 +04001734 }
Petr Borsodi80c25b12020-05-07 12:25:56 +02001735 b->offset = (u32)part->offset + ofs;
1736 b->version = node->i.version;
1737 b->ino = node->i.ino;
Petr Borsodia61d1d12020-05-07 12:25:55 +02001738 if (max_totlen < node->u.totlen)
1739 max_totlen = node->u.totlen;
Ilya Yanokddb02692008-11-13 19:49:33 +03001740 break;
1741 case JFFS2_NODETYPE_DIRENT:
1742 if (buf_ofs + buf_len < ofs + sizeof(struct
1743 jffs2_raw_dirent) +
1744 ((struct
1745 jffs2_raw_dirent *)
1746 node)->nsize) {
Mark Tomlinsonef619822015-07-01 16:38:26 +12001747 buf_len = min_t(uint32_t,
Petr Borsodia61d1d12020-05-07 12:25:55 +02001748 node->u.totlen,
Mark Tomlinsonef619822015-07-01 16:38:26 +12001749 sector_ofs +
1750 part->sector_size -
1751 ofs);
Ilya Yanokddb02692008-11-13 19:49:33 +03001752 get_fl_mem((u32)part->offset + ofs,
1753 buf_len, buf);
1754 buf_ofs = ofs;
1755 node = (void *)buf;
wdenk8886a662004-04-18 19:43:36 +00001756 }
Ilya Yanokddb02692008-11-13 19:49:33 +03001757
1758 if (!dirent_crc((struct jffs2_raw_dirent *)
1759 node) ||
1760 !dirent_name_crc(
1761 (struct
1762 jffs2_raw_dirent *)
1763 node))
1764 break;
wdenk7ad5e4c2004-04-25 15:41:35 +00001765 if (! (counterN%100))
wdenk42c05472004-03-23 22:14:11 +00001766 puts ("\b\b. ");
Petr Borsodi80c25b12020-05-07 12:25:56 +02001767 b = insert_node(&pL->dir);
1768 if (!b) {
Ilya Yanoka87011e2009-08-12 16:42:48 +04001769 free(buf);
1770 jffs2_free_cache(part);
wdenkfe8c2802002-11-03 00:38:21 +00001771 return 0;
Ilya Yanoka87011e2009-08-12 16:42:48 +04001772 }
Petr Borsodi80c25b12020-05-07 12:25:56 +02001773 b->offset = (u32)part->offset + ofs;
1774 b->version = node->d.version;
1775 b->pino = node->d.pino;
Petr Borsodia61d1d12020-05-07 12:25:55 +02001776 if (max_totlen < node->u.totlen)
1777 max_totlen = node->u.totlen;
wdenkfe8c2802002-11-03 00:38:21 +00001778 counterN++;
Ilya Yanokddb02692008-11-13 19:49:33 +03001779 break;
1780 case JFFS2_NODETYPE_CLEANMARKER:
Petr Borsodia61d1d12020-05-07 12:25:55 +02001781 if (node->u.totlen != sizeof(struct jffs2_unknown_node))
wdenk6b58f332003-03-14 20:47:52 +00001782 printf("OOPS Cleanmarker has bad size "
Wolfgang Denk509cd072008-07-14 15:06:35 +02001783 "%d != %zu\n",
Petr Borsodia61d1d12020-05-07 12:25:55 +02001784 node->u.totlen,
wdenk6b58f332003-03-14 20:47:52 +00001785 sizeof(struct jffs2_unknown_node));
Petr Borsodia61d1d12020-05-07 12:25:55 +02001786 if (node->u.totlen ==
1787 sizeof(struct jffs2_unknown_node) &&
1788 ofs == sector_ofs) {
Mark Tomlinson56a7f482015-07-01 16:38:28 +12001789 /*
1790 * Found a CLEANMARKER at the beginning
1791 * of the sector. It's in the correct
1792 * place with correct size and CRC.
1793 */
1794 clean_sector = 1;
1795 }
Ilya Yanokddb02692008-11-13 19:49:33 +03001796 break;
1797 case JFFS2_NODETYPE_PADDING:
Petr Borsodia61d1d12020-05-07 12:25:55 +02001798 if (node->u.totlen <
1799 sizeof(struct jffs2_unknown_node))
wdenk2c9b05d2003-09-10 22:30:53 +00001800 printf("OOPS Padding has bad size "
Wolfgang Denk509cd072008-07-14 15:06:35 +02001801 "%d < %zu\n",
Petr Borsodia61d1d12020-05-07 12:25:55 +02001802 node->u.totlen,
wdenk2c9b05d2003-09-10 22:30:53 +00001803 sizeof(struct jffs2_unknown_node));
Ilya Yanokddb02692008-11-13 19:49:33 +03001804 break;
Ilya Yanoka933bd62008-11-13 19:49:35 +03001805 case JFFS2_NODETYPE_SUMMARY:
1806 break;
Ilya Yanokddb02692008-11-13 19:49:33 +03001807 default:
Wolfgang Denk509cd072008-07-14 15:06:35 +02001808 printf("Unknown node type: %x len %d offset 0x%x\n",
Petr Borsodia61d1d12020-05-07 12:25:55 +02001809 node->u.nodetype,
1810 node->u.totlen, ofs);
wdenkfe8c2802002-11-03 00:38:21 +00001811 }
Petr Borsodia61d1d12020-05-07 12:25:55 +02001812 ofs += ((node->u.totlen + 3) & ~3);
wdenkfe8c2802002-11-03 00:38:21 +00001813 counterF++;
wdenkfe8c2802002-11-03 00:38:21 +00001814 }
wdenkfe8c2802002-11-03 00:38:21 +00001815 }
1816
Ilya Yanokddb02692008-11-13 19:49:33 +03001817 free(buf);
Mark Tomlinsonefdc2a62015-07-01 16:38:29 +12001818#if defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS)
1819 /*
1820 * Sort the lists.
1821 */
1822 sort_list(&pL->frag);
1823 sort_list(&pL->dir);
1824#endif
wdenkfe8c2802002-11-03 00:38:21 +00001825 putstr("\b\b done.\r\n"); /* close off the dots */
Ilya Yanoke7fd6712008-11-13 19:49:34 +03001826
1827 /* We don't care if malloc failed - then each read operation will
1828 * allocate its own buffer as necessary (NAND) or will read directly
1829 * from flash (NOR).
1830 */
1831 pL->readbuf = malloc(max_totlen);
1832
wdenkfe8c2802002-11-03 00:38:21 +00001833 /* turn the lcd back on. */
1834 /* splash(); */
1835
1836#if 0
wdenk6b58f332003-03-14 20:47:52 +00001837 putLabeledWord("dir entries = ", pL->dir.listCount);
1838 putLabeledWord("frag entries = ", pL->frag.listCount);
wdenkfe8c2802002-11-03 00:38:21 +00001839 putLabeledWord("+4 increments = ", counter4);
1840 putLabeledWord("+file_offset increments = ", counterF);
1841
1842#endif
1843
wdenk6b58f332003-03-14 20:47:52 +00001844#ifdef DEBUG_DIRENTS
1845 dump_dirents(pL);
1846#endif
wdenkfe8c2802002-11-03 00:38:21 +00001847
wdenk6b58f332003-03-14 20:47:52 +00001848#ifdef DEBUG_FRAGMENTS
1849 dump_fragments(pL);
1850#endif
wdenkfe8c2802002-11-03 00:38:21 +00001851
wdenkfe8c2802002-11-03 00:38:21 +00001852 /* give visual feedback that we are done scanning the flash */
1853 led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */
1854 return 1;
1855}
1856
1857
wdenkfe8c2802002-11-03 00:38:21 +00001858static u32
1859jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL)
1860{
1861 struct b_node *b;
wdenk8886a662004-04-18 19:43:36 +00001862 struct jffs2_raw_inode ojNode;
wdenkfe8c2802002-11-03 00:38:21 +00001863 struct jffs2_raw_inode *jNode;
1864 int i;
1865
wdenkfe8c2802002-11-03 00:38:21 +00001866 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
1867 piL->compr_info[i].num_frags = 0;
1868 piL->compr_info[i].compr_sum = 0;
1869 piL->compr_info[i].decompr_sum = 0;
1870 }
1871
wdenk6b58f332003-03-14 20:47:52 +00001872 b = pL->frag.listHead;
wdenkfe8c2802002-11-03 00:38:21 +00001873 while (b) {
wdenk8886a662004-04-18 19:43:36 +00001874 jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset,
1875 sizeof(ojNode), &ojNode);
wdenkfe8c2802002-11-03 00:38:21 +00001876 if (jNode->compr < JFFS2_NUM_COMPR) {
1877 piL->compr_info[jNode->compr].num_frags++;
1878 piL->compr_info[jNode->compr].compr_sum += jNode->csize;
1879 piL->compr_info[jNode->compr].decompr_sum += jNode->dsize;
1880 }
1881 b = b->next;
1882 }
1883 return 0;
1884}
1885
1886
wdenkfe8c2802002-11-03 00:38:21 +00001887static struct b_lists *
1888jffs2_get_list(struct part_info * part, const char *who)
1889{
Wolfgang Denk47f57792005-08-08 01:03:24 +02001890 /* copy requested part_info struct pointer to global location */
1891 current_part = part;
1892
wdenkfe8c2802002-11-03 00:38:21 +00001893 if (jffs2_1pass_rescan_needed(part)) {
1894 if (!jffs2_1pass_build_lists(part)) {
1895 printf("%s: Failed to scan JFFSv2 file structure\n", who);
1896 return NULL;
1897 }
1898 }
1899 return (struct b_lists *)part->jffs2_priv;
1900}
1901
1902
1903/* Print directory / file contents */
1904u32
1905jffs2_1pass_ls(struct part_info * part, const char *fname)
1906{
1907 struct b_lists *pl;
Wolfgang Denk79117cd2005-08-16 09:32:45 +02001908 long ret = 1;
wdenkfe8c2802002-11-03 00:38:21 +00001909 u32 inode;
1910
wdenk6b58f332003-03-14 20:47:52 +00001911 if (! (pl = jffs2_get_list(part, "ls")))
wdenkfe8c2802002-11-03 00:38:21 +00001912 return 0;
1913
1914 if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) {
1915 putstr("ls: Failed to scan jffs2 file structure\r\n");
1916 return 0;
1917 }
wdenk7ad5e4c2004-04-25 15:41:35 +00001918
1919
wdenkfe8c2802002-11-03 00:38:21 +00001920#if 0
1921 putLabeledWord("found file at inode = ", inode);
1922 putLabeledWord("read_inode returns = ", ret);
1923#endif
wdenk7ad5e4c2004-04-25 15:41:35 +00001924
1925 return ret;
wdenkfe8c2802002-11-03 00:38:21 +00001926}
1927
1928
wdenkfe8c2802002-11-03 00:38:21 +00001929/* Load a file from flash into memory. fname can be a full path */
1930u32
1931jffs2_1pass_load(char *dest, struct part_info * part, const char *fname)
1932{
1933
1934 struct b_lists *pl;
Wolfgang Denk79117cd2005-08-16 09:32:45 +02001935 long ret = 1;
wdenkfe8c2802002-11-03 00:38:21 +00001936 u32 inode;
1937
1938 if (! (pl = jffs2_get_list(part, "load")))
1939 return 0;
1940
1941 if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) {
1942 putstr("load: Failed to find inode\r\n");
1943 return 0;
1944 }
1945
1946 /* Resolve symlinks */
1947 if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) {
1948 putstr("load: Failed to resolve inode structure\r\n");
1949 return 0;
1950 }
1951
1952 if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) {
1953 putstr("load: Failed to read inode\r\n");
1954 return 0;
1955 }
1956
1957 DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname,
1958 (unsigned long) dest, ret);
1959 return ret;
1960}
1961
1962/* Return information about the fs on this partition */
1963u32
1964jffs2_1pass_info(struct part_info * part)
1965{
1966 struct b_jffs2_info info;
1967 struct b_lists *pl;
1968 int i;
1969
1970 if (! (pl = jffs2_get_list(part, "info")))
1971 return 0;
1972
1973 jffs2_1pass_fill_info(pl, &info);
wdenk6b58f332003-03-14 20:47:52 +00001974 for (i = 0; i < JFFS2_NUM_COMPR; i++) {
wdenk42c05472004-03-23 22:14:11 +00001975 printf ("Compression: %s\n"
1976 "\tfrag count: %d\n"
1977 "\tcompressed sum: %d\n"
1978 "\tuncompressed sum: %d\n",
1979 compr_names[i],
1980 info.compr_info[i].num_frags,
1981 info.compr_info[i].compr_sum,
1982 info.compr_info[i].decompr_sum);
wdenkfe8c2802002-11-03 00:38:21 +00001983 }
1984 return 1;
1985}