wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1 | /* |
| 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 Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 55 | * (1) most pages are 4096 bytes |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 56 | * (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 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 78 | /* |
| 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-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 95 | * The fragment sorting feature must be enabled by CONFIG_SYS_JFFS2_SORT_FRAGMENTS. |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 96 | * 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 |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 109 | * might be incompatible with any new code from the jffs2 maintainers anyway, |
| 110 | * it should probably be dumped and replaced by something like jffs2reader! |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 111 | */ |
| 112 | |
| 113 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 114 | #include <config.h> |
| 115 | #include <malloc.h> |
Tom Rini | 3cd84d6 | 2013-12-05 14:48:38 -0500 | [diff] [blame] | 116 | #include <div64.h> |
Tom Rini | 7c4734d | 2016-03-27 14:48:36 -0400 | [diff] [blame] | 117 | #include <linux/compiler.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 118 | #include <linux/stat.h> |
| 119 | #include <linux/time.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 120 | #include <u-boot/crc.h> |
Stuart Wood | 471a2f0 | 2008-06-02 16:40:08 -0400 | [diff] [blame] | 121 | #include <watchdog.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 122 | #include <jffs2/jffs2.h> |
| 123 | #include <jffs2/jffs2_1pass.h> |
Mike Frysinger | 11d1a09 | 2012-04-09 13:39:55 +0000 | [diff] [blame] | 124 | #include <linux/compat.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 125 | #include <linux/errno.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 126 | |
| 127 | #include "jffs2_private.h" |
| 128 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 129 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 130 | #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */ |
| 131 | #define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 132 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 133 | /* Debugging switches */ |
| 134 | #undef DEBUG_DIRENTS /* print directory entry list after scan */ |
| 135 | #undef DEBUG_FRAGMENTS /* print fragment list after scan */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 136 | #undef DEBUG /* enable debugging messages */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 137 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 138 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 139 | #ifdef DEBUG |
| 140 | # define DEBUGF(fmt,args...) printf(fmt ,##args) |
| 141 | #else |
| 142 | # define DEBUGF(fmt,args...) |
| 143 | #endif |
| 144 | |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 145 | #include "summary.h" |
| 146 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 147 | /* keeps pointer to currentlu processed partition */ |
| 148 | static struct part_info *current_part; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 149 | |
Wolfgang Denk | 15888b4 | 2007-07-05 17:56:27 +0200 | [diff] [blame] | 150 | #if (defined(CONFIG_JFFS2_NAND) && \ |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 151 | defined(CONFIG_CMD_NAND) ) |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 152 | #include <nand.h> |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 153 | /* |
| 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 | |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 163 | #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 | |
| 172 | static u8* nand_cache = NULL; |
| 173 | static u32 nand_cache_off = (u32)-1; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 174 | |
| 175 | static int read_nand_cached(u32 off, u32 size, u_char *buf) |
| 176 | { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 177 | struct mtdids *id = current_part->dev->id; |
Grygorii Strashko | ac8f8e3 | 2017-06-26 19:12:57 -0500 | [diff] [blame] | 178 | struct mtd_info *mtd; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 179 | u32 bytes_read = 0; |
Marian Balakowicz | 6a07675 | 2006-04-08 19:08:06 +0200 | [diff] [blame] | 180 | size_t retlen; |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 181 | size_t toread; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 182 | int cpy_bytes; |
| 183 | |
Grygorii Strashko | ac8f8e3 | 2017-06-26 19:12:57 -0500 | [diff] [blame] | 184 | mtd = get_nand_dev_by_index(id->num); |
| 185 | if (!mtd) |
| 186 | return -1; |
| 187 | |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 188 | while (bytes_read < size) { |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 189 | retlen = NAND_CACHE_SIZE; |
| 190 | if( nand_cache_off + retlen > mtd->size ) |
| 191 | retlen = mtd->size - nand_cache_off; |
| 192 | |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 193 | if ((off + bytes_read < nand_cache_off) || |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 194 | (off + bytes_read >= nand_cache_off + retlen)) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 195 | 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 */ |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 199 | nand_cache = malloc(NAND_CACHE_SIZE); |
| 200 | if (!nand_cache) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 201 | printf("read_nand_cached: can't alloc cache size %d bytes\n", |
| 202 | NAND_CACHE_SIZE); |
| 203 | return -1; |
| 204 | } |
| 205 | } |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 206 | |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 207 | toread = NAND_CACHE_SIZE; |
| 208 | if( nand_cache_off + toread > mtd->size ) |
| 209 | toread = mtd->size - nand_cache_off; |
| 210 | |
| 211 | retlen = toread; |
Grygorii Strashko | ac8f8e3 | 2017-06-26 19:12:57 -0500 | [diff] [blame] | 212 | if (nand_read(mtd, nand_cache_off, |
Engling, Uwe | e5e23ee | 2017-10-10 14:20:55 +0000 | [diff] [blame] | 213 | &retlen, nand_cache) < 0 || |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 214 | retlen != toread) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 215 | printf("read_nand_cached: error reading nand off %#x size %d bytes\n", |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 216 | nand_cache_off, toread); |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 217 | return -1; |
| 218 | } |
| 219 | } |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 220 | cpy_bytes = nand_cache_off + retlen - (off + bytes_read); |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 221 | 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 Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 231 | static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf) |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 232 | { |
| 233 | u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size); |
| 234 | |
| 235 | if (NULL == buf) { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 236 | printf("get_fl_mem_nand: can't alloc %d bytes\n", size); |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 237 | return NULL; |
| 238 | } |
| 239 | if (read_nand_cached(off, size, buf) < 0) { |
wdenk | f248ebc | 2004-05-05 19:44:41 +0000 | [diff] [blame] | 240 | if (!ext_buf) |
| 241 | free(buf); |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | return buf; |
| 246 | } |
| 247 | |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 248 | static void *get_node_mem_nand(u32 off, void *ext_buf) |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 249 | { |
| 250 | struct jffs2_unknown_node node; |
| 251 | void *ret = NULL; |
| 252 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 253 | if (NULL == get_fl_mem_nand(off, sizeof(node), &node)) |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 254 | return NULL; |
| 255 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 256 | if (!(ret = get_fl_mem_nand(off, node.magic == |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 257 | JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node), |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 258 | ext_buf))) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 259 | 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 Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 265 | static void put_fl_mem_nand(void *buf) |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 266 | { |
| 267 | free(buf); |
| 268 | } |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 269 | #endif |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 270 | |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 271 | #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 | |
| 286 | static u8* onenand_cache; |
| 287 | static u32 onenand_cache_off = (u32)-1; |
| 288 | |
| 289 | static int read_onenand_cached(u32 off, u32 size, u_char *buf) |
| 290 | { |
| 291 | u32 bytes_read = 0; |
| 292 | size_t retlen; |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 293 | size_t toread; |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 294 | int cpy_bytes; |
| 295 | |
| 296 | while (bytes_read < size) { |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 297 | retlen = ONENAND_CACHE_SIZE; |
| 298 | if( onenand_cache_off + retlen > onenand_mtd.size ) |
| 299 | retlen = onenand_mtd.size - onenand_cache_off; |
| 300 | |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 301 | if ((off + bytes_read < onenand_cache_off) || |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 302 | (off + bytes_read >= onenand_cache_off + retlen)) { |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 303 | 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 Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 315 | 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 Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 319 | if (onenand_read(&onenand_mtd, onenand_cache_off, retlen, |
Engling, Uwe | e5e23ee | 2017-10-10 14:20:55 +0000 | [diff] [blame] | 320 | &retlen, onenand_cache) < 0 || |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 321 | retlen != toread) { |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 322 | printf("read_onenand_cached: error reading nand off %#x size %d bytes\n", |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 323 | onenand_cache_off, toread); |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 324 | return -1; |
| 325 | } |
| 326 | } |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 327 | cpy_bytes = onenand_cache_off + retlen - (off + bytes_read); |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 328 | 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 | |
| 338 | static 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 Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 355 | static void *get_node_mem_onenand(u32 off, void *ext_buf) |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 356 | { |
| 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 Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 365 | ext_buf); |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 366 | 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 | |
| 374 | static void put_fl_mem_onenand(void *buf) |
| 375 | { |
| 376 | free(buf); |
| 377 | } |
| 378 | #endif |
| 379 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 380 | |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 381 | #if defined(CONFIG_CMD_FLASH) |
Tom Rini | 063c938 | 2022-07-23 13:05:03 -0400 | [diff] [blame] | 382 | #include <flash.h> |
| 383 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 384 | /* |
| 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 Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 390 | static inline void *get_fl_mem_nor(u32 off, u32 size, void *ext_buf) |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 391 | { |
| 392 | u32 addr = off; |
| 393 | struct mtdids *id = current_part->dev->id; |
| 394 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 395 | flash_info_t *flash = &flash_info[id->num]; |
| 396 | |
| 397 | addr += flash->start[0]; |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 398 | if (ext_buf) { |
| 399 | memcpy(ext_buf, (void *)addr, size); |
| 400 | return ext_buf; |
| 401 | } |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 402 | return (void*)addr; |
| 403 | } |
| 404 | |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 405 | static inline void *get_node_mem_nor(u32 off, void *ext_buf) |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 406 | { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 407 | struct jffs2_unknown_node *pNode; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 408 | |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 409 | /* 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 Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 414 | } |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 415 | #endif |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 416 | |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 417 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 418 | /* |
Wolfgang Denk | eb95c85 | 2005-08-10 15:14:32 +0200 | [diff] [blame] | 419 | * Generic jffs2 raw memory and node read routines. |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 420 | * |
| 421 | */ |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 422 | static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf) |
| 423 | { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 424 | struct mtdids *id = current_part->dev->id; |
Wolfgang Denk | eb95c85 | 2005-08-10 15:14:32 +0200 | [diff] [blame] | 425 | |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 426 | switch(id->type) { |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 427 | #if defined(CONFIG_CMD_FLASH) |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 428 | case MTD_DEV_TYPE_NOR: |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 429 | return get_fl_mem_nor(off, size, ext_buf); |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 430 | break; |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 431 | #endif |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 432 | #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND) |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 433 | case MTD_DEV_TYPE_NAND: |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 434 | return get_fl_mem_nand(off, size, ext_buf); |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 435 | break; |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 436 | #endif |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 437 | #if defined(CONFIG_CMD_ONENAND) |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 438 | case MTD_DEV_TYPE_ONENAND: |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 439 | return get_fl_mem_onenand(off, size, ext_buf); |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 440 | break; |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 441 | #endif |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 442 | default: |
| 443 | printf("get_fl_mem: unknown device type, " \ |
| 444 | "using raw offset!\n"); |
| 445 | } |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 446 | return (void*)off; |
| 447 | } |
| 448 | |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 449 | static inline void *get_node_mem(u32 off, void *ext_buf) |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 450 | { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 451 | struct mtdids *id = current_part->dev->id; |
Wolfgang Denk | eb95c85 | 2005-08-10 15:14:32 +0200 | [diff] [blame] | 452 | |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 453 | switch(id->type) { |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 454 | #if defined(CONFIG_CMD_FLASH) |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 455 | case MTD_DEV_TYPE_NOR: |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 456 | return get_node_mem_nor(off, ext_buf); |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 457 | break; |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 458 | #endif |
Wolfgang Denk | 15888b4 | 2007-07-05 17:56:27 +0200 | [diff] [blame] | 459 | #if defined(CONFIG_JFFS2_NAND) && \ |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 460 | defined(CONFIG_CMD_NAND) |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 461 | case MTD_DEV_TYPE_NAND: |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 462 | return get_node_mem_nand(off, ext_buf); |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 463 | break; |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 464 | #endif |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 465 | #if defined(CONFIG_CMD_ONENAND) |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 466 | case MTD_DEV_TYPE_ONENAND: |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 467 | return get_node_mem_onenand(off, ext_buf); |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 468 | break; |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 469 | #endif |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 470 | default: |
| 471 | printf("get_fl_mem: unknown device type, " \ |
| 472 | "using raw offset!\n"); |
| 473 | } |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 474 | return (void*)off; |
| 475 | } |
| 476 | |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 477 | static inline void put_fl_mem(void *buf, void *ext_buf) |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 478 | { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 479 | struct mtdids *id = current_part->dev->id; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 480 | |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 481 | /* 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 Wood | c97ba11 | 2008-10-31 13:51:12 -0500 | [diff] [blame] | 485 | switch (id->type) { |
| 486 | #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND) |
| 487 | case MTD_DEV_TYPE_NAND: |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 488 | return put_fl_mem_nand(buf); |
| 489 | #endif |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 490 | #if defined(CONFIG_CMD_ONENAND) |
Scott Wood | c97ba11 | 2008-10-31 13:51:12 -0500 | [diff] [blame] | 491 | case MTD_DEV_TYPE_ONENAND: |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 492 | return put_fl_mem_onenand(buf); |
| 493 | #endif |
Scott Wood | c97ba11 | 2008-10-31 13:51:12 -0500 | [diff] [blame] | 494 | } |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 495 | } |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 496 | |
| 497 | /* Compression names */ |
| 498 | static char *compr_names[] = { |
| 499 | "NONE", |
| 500 | "ZERO", |
| 501 | "RTIME", |
| 502 | "RUBINMIPS", |
| 503 | "COPY", |
| 504 | "DYNRUBIN", |
wdenk | e84ec90 | 2005-05-05 00:04:14 +0000 | [diff] [blame] | 505 | "ZLIB", |
Wolfgang Denk | 0eded4f | 2010-01-15 11:10:33 +0100 | [diff] [blame] | 506 | #if defined(CONFIG_JFFS2_LZO) |
wdenk | e84ec90 | 2005-05-05 00:04:14 +0000 | [diff] [blame] | 507 | "LZO", |
wdenk | e84ec90 | 2005-05-05 00:04:14 +0000 | [diff] [blame] | 508 | #endif |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 509 | }; |
| 510 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 511 | /* Memory management */ |
| 512 | struct mem_block { |
| 513 | u32 index; |
| 514 | struct mem_block *next; |
| 515 | struct b_node nodes[NODE_CHUNK]; |
| 516 | }; |
| 517 | |
| 518 | |
| 519 | static void |
| 520 | free_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 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 528 | |
| 529 | static struct b_node * |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 530 | add_node(struct b_list *list) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 531 | { |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 532 | u32 index = 0; |
| 533 | struct mem_block *memBase; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 534 | struct b_node *b; |
| 535 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 536 | memBase = list->listMemBase; |
| 537 | if (memBase != NULL) |
| 538 | index = memBase->index; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 539 | #if 0 |
| 540 | putLabeledWord("add_node: index = ", index); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 541 | putLabeledWord("add_node: memBase = ", list->listMemBase); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 542 | #endif |
| 543 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 544 | 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) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 548 | putstr("add_node: malloc failed\n"); |
| 549 | return NULL; |
| 550 | } |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 551 | memBase->next = list->listMemBase; |
| 552 | index = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 553 | #if 0 |
| 554 | putLabeledWord("add_node: alloced a new membase at ", *memBase); |
| 555 | #endif |
| 556 | |
| 557 | } |
| 558 | /* now we have room to add it. */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 559 | b = &memBase->nodes[index]; |
| 560 | index ++; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 561 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 562 | memBase->index = index; |
| 563 | list->listMemBase = memBase; |
| 564 | list->listCount++; |
| 565 | return b; |
| 566 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 567 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 568 | static struct b_node * |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 569 | insert_node(struct b_list *list) |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 570 | { |
| 571 | struct b_node *new; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 572 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 573 | if (!(new = add_node(list))) { |
| 574 | putstr("add_node failed!\r\n"); |
| 575 | return NULL; |
| 576 | } |
Mark Tomlinson | efdc2a6 | 2015-07-01 16:38:29 +1200 | [diff] [blame] | 577 | new->next = NULL; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 578 | |
Mark Tomlinson | efdc2a6 | 2015-07-01 16:38:29 +1200 | [diff] [blame] | 579 | if (list->listTail != NULL) |
| 580 | list->listTail->next = new; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 581 | else |
Mark Tomlinson | efdc2a6 | 2015-07-01 16:38:29 +1200 | [diff] [blame] | 582 | list->listHead = new; |
| 583 | list->listTail = new; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 584 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 585 | return new; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 588 | #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 589 | /* Sort data entries with the latest version last, so that if there |
| 590 | * is overlapping data the latest version will be used. |
| 591 | */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 592 | static int compare_inodes(struct b_node *new, struct b_node *old) |
| 593 | { |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 594 | return new->version > old->version; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 595 | } |
| 596 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 597 | /* 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 | */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 602 | static int compare_dirents(struct b_node *new, struct b_node *old) |
| 603 | { |
Mark Tomlinson | 7c6558e | 2015-07-01 16:38:23 +1200 | [diff] [blame] | 604 | /* |
| 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. |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 608 | */ |
Mark Tomlinson | 7c6558e | 2015-07-01 16:38:23 +1200 | [diff] [blame] | 609 | 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; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 613 | |
Mark Tomlinson | 7c6558e | 2015-07-01 16:38:23 +1200 | [diff] [blame] | 614 | 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. |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 621 | */ |
Mark Tomlinson | 7c6558e | 2015-07-01 16:38:23 +1200 | [diff] [blame] | 622 | 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 | } |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 638 | } |
Mark Tomlinson | 7c6558e | 2015-07-01 16:38:23 +1200 | [diff] [blame] | 639 | put_fl_mem(jNew, NULL); |
| 640 | put_fl_mem(jOld, NULL); |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 641 | |
Mark Tomlinson | 7c6558e | 2015-07-01 16:38:23 +1200 | [diff] [blame] | 642 | return ret; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 643 | } |
| 644 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 645 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 646 | void |
| 647 | jffs2_free_cache(struct part_info *part) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 648 | { |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 649 | 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 Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 655 | free(pL->readbuf); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 656 | free(pL); |
| 657 | } |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | static u32 |
| 661 | jffs_init_1pass_list(struct part_info *part) |
| 662 | { |
| 663 | struct b_lists *pL; |
| 664 | |
| 665 | jffs2_free_cache(part); |
| 666 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 667 | if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) { |
| 668 | pL = (struct b_lists *)part->jffs2_priv; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 669 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 670 | memset(pL, 0, sizeof(*pL)); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 671 | #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 672 | pL->dir.listCompare = compare_dirents; |
| 673 | pL->frag.listCompare = compare_inodes; |
| 674 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 675 | } |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | /* find the inode from the slashless name given a parent */ |
| 680 | static long |
| 681 | jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest) |
| 682 | { |
| 683 | struct b_node *b; |
| 684 | struct jffs2_raw_inode *jNode; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 685 | u32 totalSize = 0; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 686 | u32 latestVersion = 0; |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 687 | uchar *lDest; |
| 688 | uchar *src; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 689 | int i; |
| 690 | u32 counter = 0; |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 691 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 692 | /* 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 Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 699 | int latestOffset = -1; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 700 | for (b = pL->frag.listHead; b != NULL; b = b->next) { |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 701 | if (inode == b->ino) { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 702 | /* get actual file length from the newest node */ |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 703 | if (b->version >= latestVersion) { |
| 704 | latestVersion = b->version; |
| 705 | latestOffset = b->offset; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 708 | } |
| 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 Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 714 | put_fl_mem(jNode, pL->readbuf); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 715 | } |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 716 | |
Mark Tomlinson | 6d99845 | 2015-07-01 16:38:22 +1200 | [diff] [blame] | 717 | /* |
| 718 | * If no destination is provided, we are done. |
| 719 | * Just return the total size. |
| 720 | */ |
| 721 | if (!dest) |
| 722 | return totalSize; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 723 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 724 | for (b = pL->frag.listHead; b != NULL; b = b->next) { |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 725 | 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); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 733 | #if 0 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 734 | 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); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 744 | #endif |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 745 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 746 | if(dest) { |
Mark Tomlinson | b71748f | 2015-07-01 16:38:25 +1200 | [diff] [blame] | 747 | /* |
| 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); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 756 | /* ignore data behind latest known EOF */ |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 757 | if (jNode->offset > totalSize) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 758 | put_fl_mem(jNode, pL->readbuf); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 759 | continue; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 760 | } |
Ilya Yanok | c0c0773 | 2008-11-13 19:49:36 +0300 | [diff] [blame] | 761 | if (b->datacrc == CRC_UNKNOWN) |
| 762 | b->datacrc = data_crc(jNode) ? |
| 763 | CRC_OK : CRC_BAD; |
| 764 | if (b->datacrc == CRC_BAD) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 765 | put_fl_mem(jNode, pL->readbuf); |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 766 | continue; |
| 767 | } |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 768 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 769 | lDest = (uchar *) (dest + jNode->offset); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 770 | #if 0 |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 771 | putLabeledWord("read_inode: src = ", src); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 772 | putLabeledWord("read_inode: dest = ", lDest); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 773 | #endif |
| 774 | switch (jNode->compr) { |
| 775 | case JFFS2_COMPR_NONE: |
Wolfgang Denk | bf913c1 | 2011-10-05 22:58:11 +0200 | [diff] [blame] | 776 | ldr_memcpy(lDest, src, jNode->dsize); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 777 | break; |
| 778 | case JFFS2_COMPR_ZERO: |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 779 | for (i = 0; i < jNode->dsize; i++) |
| 780 | *(lDest++) = 0; |
| 781 | break; |
| 782 | case JFFS2_COMPR_RTIME: |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 783 | rtime_decompress(src, lDest, jNode->csize, jNode->dsize); |
| 784 | break; |
| 785 | case JFFS2_COMPR_DYNRUBIN: |
| 786 | /* this is slow but it works */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 787 | dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize); |
| 788 | break; |
| 789 | case JFFS2_COMPR_ZLIB: |
Wolfgang Denk | bf913c1 | 2011-10-05 22:58:11 +0200 | [diff] [blame] | 790 | zlib_decompress(src, lDest, jNode->csize, jNode->dsize); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 791 | break; |
Wolfgang Denk | 0eded4f | 2010-01-15 11:10:33 +0100 | [diff] [blame] | 792 | #if defined(CONFIG_JFFS2_LZO) |
wdenk | e84ec90 | 2005-05-05 00:04:14 +0000 | [diff] [blame] | 793 | case JFFS2_COMPR_LZO: |
Wolfgang Denk | bf913c1 | 2011-10-05 22:58:11 +0200 | [diff] [blame] | 794 | lzo_decompress(src, lDest, jNode->csize, jNode->dsize); |
wdenk | e84ec90 | 2005-05-05 00:04:14 +0000 | [diff] [blame] | 795 | break; |
| 796 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 797 | default: |
| 798 | /* unknown */ |
Loïc Minier | 5d0569a | 2011-02-03 22:04:26 +0100 | [diff] [blame] | 799 | putLabeledWord("UNKNOWN COMPRESSION METHOD = ", jNode->compr); |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 800 | put_fl_mem(jNode, pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 801 | return -1; |
| 802 | break; |
| 803 | } |
| 804 | } |
| 805 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 806 | #if 0 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 807 | putLabeledWord("read_inode: totalSize = ", totalSize); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 808 | #endif |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 809 | put_fl_mem(jNode, pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 810 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 811 | counter++; |
| 812 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 813 | |
| 814 | #if 0 |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 815 | putLabeledWord("read_inode: returning = ", totalSize); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 816 | #endif |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 817 | return totalSize; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /* find the inode from the slashless name given a parent */ |
| 821 | static u32 |
| 822 | jffs2_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 */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 836 | for(b = pL->dir.listHead; b; b = b->next, counter++) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 837 | jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset, |
| 838 | pL->readbuf); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 839 | if ((pino == jDir->pino) && (len == jDir->nsize) && |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 840 | (!strncmp((char *)jDir->name, name, len))) { /* a match */ |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 841 | if (jDir->version < version) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 842 | put_fl_mem(jDir, pL->readbuf); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 843 | continue; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 844 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 845 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 846 | if (jDir->version == version && inode != 0) { |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 847 | /* I'm pretty sure this isn't legal */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 848 | 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 Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 864 | put_fl_mem(jDir, pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 865 | } |
| 866 | return inode; |
| 867 | } |
| 868 | |
wdenk | ad276f2 | 2004-01-04 16:28:35 +0000 | [diff] [blame] | 869 | char *mkmodestr(unsigned long mode, char *str) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 870 | { |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 871 | static const char *l = "xwr"; |
| 872 | int mask = 1, i; |
| 873 | char c; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 874 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 875 | 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 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 885 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 886 | for(i = 0; i < 9; i++) { |
| 887 | c = l[i%3]; |
| 888 | str[9-i] = (mode & mask)?c:'-'; |
| 889 | mask = mask<<1; |
| 890 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 891 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 892 | 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; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | static inline void dump_stat(struct stat *st, const char *name) |
| 900 | { |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 901 | char str[20]; |
| 902 | char s[64], *p; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 903 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 904 | if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */ |
| 905 | st->st_mtime = 1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 906 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 907 | ctime_r((time_t *)&st->st_mtime, s/*,64*/); /* newlib ctime doesn't have buflen */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 908 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 909 | if ((p = strchr(s,'\n')) != NULL) *p = '\0'; |
| 910 | if ((p = strchr(s,'\r')) != NULL) *p = '\0'; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 911 | |
| 912 | /* |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 913 | printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str), |
| 914 | st->st_size, s, name); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 915 | */ |
| 916 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 917 | printf(" %s %8ld %s %s", mkmodestr(st->st_mode,str), st->st_size, s, name); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static 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 Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 927 | strncpy(fname, (char *)d->name, d->nsize); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 928 | fname[d->nsize] = '\0'; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 929 | |
| 930 | memset(&st,0,sizeof(st)); |
| 931 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 932 | st.st_mtime = i->mtime; |
| 933 | st.st_mode = i->mode; |
| 934 | st.st_ino = i->ino; |
Ilya Yanok | c4785fb | 2008-11-13 19:49:31 +0300 | [diff] [blame] | 935 | st.st_size = i->isize; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 936 | |
| 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 */ |
| 951 | static u32 |
| 952 | jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino) |
| 953 | { |
| 954 | struct b_node *b; |
| 955 | struct jffs2_raw_dirent *jDir; |
| 956 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 957 | for (b = pL->dir.listHead; b; b = b->next) { |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 958 | if (pino == b->pino) { |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 959 | u32 i_version = 0; |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 960 | int i_offset = -1; |
| 961 | struct jffs2_raw_inode *jNode = NULL; |
Mark Tomlinson | 2946852 | 2015-07-01 16:38:24 +1200 | [diff] [blame] | 962 | struct b_node *b2; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 963 | |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 964 | jDir = (struct jffs2_raw_dirent *) |
| 965 | get_node_mem(b->offset, pL->readbuf); |
Mark Tomlinson | 2946852 | 2015-07-01 16:38:24 +1200 | [diff] [blame] | 966 | #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 Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 997 | if (b2->ino == jDir->ino && |
| 998 | b2->version >= i_version) { |
| 999 | i_version = b2->version; |
| 1000 | i_offset = b2->offset; |
wdenk | f248ebc | 2004-05-05 19:44:41 +0000 | [diff] [blame] | 1001 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1004 | 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); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | return pino; |
| 1020 | } |
| 1021 | |
| 1022 | static u32 |
| 1023 | jffs2_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 | |
| 1078 | static u32 |
| 1079 | jffs2_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; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1085 | u8 jDirFoundType = 0; |
| 1086 | u32 jDirFoundIno = 0; |
| 1087 | u32 jDirFoundPino = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1088 | 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 */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1094 | for(b = pL->dir.listHead; b; b = b->next) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1095 | jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset, |
| 1096 | pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1097 | if (ino == jDir->ino) { |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1098 | if (jDir->version < version) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1099 | put_fl_mem(jDir, pL->readbuf); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1100 | continue; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1101 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1102 | |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1103 | if (jDir->version == version && jDirFoundType) { |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1104 | /* I'm pretty sure this isn't legal */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1105 | putstr(" ** ERROR ** "); |
| 1106 | putnstr(jDir->name, jDir->nsize); |
| 1107 | putLabeledWord(" has dup version (resolve) = ", |
| 1108 | version); |
| 1109 | } |
| 1110 | |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1111 | jDirFoundType = jDir->type; |
| 1112 | jDirFoundIno = jDir->ino; |
| 1113 | jDirFoundPino = jDir->pino; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1114 | version = jDir->version; |
| 1115 | } |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1116 | put_fl_mem(jDir, pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1117 | } |
| 1118 | /* now we found the right entry again. (shoulda returned inode*) */ |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1119 | if (jDirFoundType != DT_LNK) |
| 1120 | return jDirFoundIno; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1121 | |
| 1122 | /* it's a soft link so we follow it again. */ |
| 1123 | b2 = pL->frag.listHead; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1124 | while (b2) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1125 | jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset, |
| 1126 | pL->readbuf); |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1127 | if (jNode->ino == jDirFoundIno) { |
wdenk | 6c59edc | 2004-05-03 20:45:30 +0000 | [diff] [blame] | 1128 | src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1129 | |
| 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 Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 1136 | strncpy(tmp, (char *)src, jNode->dsize); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1137 | tmp[jNode->dsize] = '\0'; |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1138 | put_fl_mem(jNode, pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1139 | break; |
| 1140 | } |
| 1141 | b2 = b2->next; |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1142 | put_fl_mem(jNode, pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1143 | } |
| 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 |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1149 | pino = jDirFoundPino; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1150 | |
| 1151 | return jffs2_1pass_search_inode(pL, tmp, pino); |
| 1152 | } |
| 1153 | |
| 1154 | static u32 |
| 1155 | jffs2_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 */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1176 | if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) && |
| 1177 | (working_tmp[0])) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1178 | 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 | |
| 1202 | unsigned char |
| 1203 | jffs2_1pass_rescan_needed(struct part_info *part) |
| 1204 | { |
| 1205 | struct b_node *b; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1206 | struct jffs2_unknown_node onode; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1207 | struct jffs2_unknown_node *node; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1208 | struct b_lists *pL = (struct b_lists *)part->jffs2_priv; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1209 | |
| 1210 | if (part->jffs2_priv == 0){ |
| 1211 | DEBUGF ("rescan: First time in use\n"); |
| 1212 | return 1; |
| 1213 | } |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 1214 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1215 | /* if we have no list, we need to rescan */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1216 | if (pL->frag.listCount == 0) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1217 | DEBUGF ("rescan: fraglist zero\n"); |
| 1218 | return 1; |
| 1219 | } |
| 1220 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1221 | /* but suppose someone reflashed a partition at the same offset... */ |
| 1222 | b = pL->dir.listHead; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1223 | while (b) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1224 | node = (struct jffs2_unknown_node *) get_fl_mem(b->offset, |
| 1225 | sizeof(onode), &onode); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1226 | if (node->nodetype != JFFS2_NODETYPE_DIRENT) { |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1227 | DEBUGF ("rescan: fs changed beneath me? (%lx)\n", |
| 1228 | (unsigned long) b->offset); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1229 | return 1; |
| 1230 | } |
| 1231 | b = b->next; |
| 1232 | } |
| 1233 | return 0; |
| 1234 | } |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1235 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1236 | #ifdef CONFIG_JFFS2_SUMMARY |
| 1237 | static 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 | |
| 1247 | static 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 Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1257 | #define dbg_summary(...) do {} while (0); |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1258 | /* |
| 1259 | * Process the stored summary information - helper function for |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1260 | * jffs2_sum_scan_sumnode() |
| 1261 | */ |
| 1262 | |
| 1263 | static 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 Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1268 | int i, pass; |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1269 | struct b_node *b; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1270 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1271 | for (pass = 0; pass < 2; pass++) { |
| 1272 | sp = summary->sum; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1273 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1274 | 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 Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1277 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1278 | switch (sum_get_unaligned16(&spu->nodetype)) { |
| 1279 | case JFFS2_NODETYPE_INODE: { |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1280 | struct jffs2_sum_inode_flash *spi; |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1281 | if (pass) { |
| 1282 | spi = sp; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1283 | |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1284 | b = insert_node(&pL->frag); |
| 1285 | if (!b) |
| 1286 | return -1; |
| 1287 | b->offset = (u32)part->offset + |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1288 | offset + |
| 1289 | sum_get_unaligned32( |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1290 | &spi->offset); |
| 1291 | b->version = sum_get_unaligned32( |
| 1292 | &spi->version); |
| 1293 | b->ino = sum_get_unaligned32( |
| 1294 | &spi->inode); |
Wagner Popov dos Santos | b9e3b67 | 2021-02-23 00:49:00 -0300 | [diff] [blame] | 1295 | b->datacrc = CRC_UNKNOWN; |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1296 | } |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1297 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1298 | sp += JFFS2_SUMMARY_INODE_SIZE; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1299 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1300 | break; |
| 1301 | } |
| 1302 | case JFFS2_NODETYPE_DIRENT: { |
| 1303 | struct jffs2_sum_dirent_flash *spd; |
| 1304 | spd = sp; |
| 1305 | if (pass) { |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1306 | b = insert_node(&pL->dir); |
| 1307 | if (!b) |
| 1308 | return -1; |
| 1309 | b->offset = (u32)part->offset + |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1310 | offset + |
| 1311 | sum_get_unaligned32( |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1312 | &spd->offset); |
| 1313 | b->version = sum_get_unaligned32( |
| 1314 | &spd->version); |
| 1315 | b->pino = sum_get_unaligned32( |
| 1316 | &spd->pino); |
Wagner Popov dos Santos | b9e3b67 | 2021-02-23 00:49:00 -0300 | [diff] [blame] | 1317 | b->datacrc = CRC_UNKNOWN; |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1318 | } |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1319 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1320 | sp += JFFS2_SUMMARY_DIRENT_SIZE( |
| 1321 | spd->nsize); |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1322 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1323 | 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 Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | /* Process the summary node - called from jffs2_scan_eraseblock() */ |
| 1343 | int 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 Rini | 7c4734d | 2016-03-27 14:48:36 -0400 | [diff] [blame] | 1348 | int ret, __maybe_unused ofs; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1349 | 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 Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1393 | if (ret == -EBADMSG) |
| 1394 | return 0; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1395 | if (ret) |
| 1396 | return ret; /* real error */ |
| 1397 | |
| 1398 | return 1; |
| 1399 | |
| 1400 | crc_err: |
| 1401 | putstr("Summary node crc error, skipping summary information.\n"); |
| 1402 | |
| 1403 | return 0; |
| 1404 | } |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1405 | #endif /* CONFIG_JFFS2_SUMMARY */ |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1406 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1407 | #ifdef DEBUG_FRAGMENTS |
| 1408 | static void |
| 1409 | dump_fragments(struct b_lists *pL) |
| 1410 | { |
| 1411 | struct b_node *b; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1412 | struct jffs2_raw_inode ojNode; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1413 | 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) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1418 | jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, |
| 1419 | sizeof(ojNode), &ojNode); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1420 | 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); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1432 | putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1433 | b = b->next; |
| 1434 | } |
| 1435 | } |
| 1436 | #endif |
| 1437 | |
| 1438 | #ifdef DEBUG_DIRENTS |
| 1439 | static void |
| 1440 | dump_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 Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1448 | jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset, |
| 1449 | pL->readbuf); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1450 | 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 Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1463 | putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1464 | b = b->next; |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1465 | put_fl_mem(jDir, pL->readbuf); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1466 | } |
| 1467 | } |
| 1468 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1469 | |
Mark Tomlinson | f89bfd0 | 2015-07-01 16:38:27 +1200 | [diff] [blame] | 1470 | #define DEFAULT_EMPTY_SCAN_SIZE 256 |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1471 | |
| 1472 | static 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 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1480 | static u32 |
| 1481 | jffs2_1pass_build_lists(struct part_info * part) |
| 1482 | { |
| 1483 | struct b_lists *pL; |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1484 | union jffs2_node_union *node; |
Tom Rini | 3cd84d6 | 2013-12-05 14:48:38 -0500 | [diff] [blame] | 1485 | u32 nr_sectors; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1486 | u32 i; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1487 | u32 counter4 = 0; |
| 1488 | u32 counterF = 0; |
| 1489 | u32 counterN = 0; |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1490 | u32 max_totlen = 0; |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1491 | u32 buf_size; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1492 | char *buf; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1493 | |
Tom Rini | 3cd84d6 | 2013-12-05 14:48:38 -0500 | [diff] [blame] | 1494 | nr_sectors = lldiv(part->size, part->sector_size); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1495 | /* 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. */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1501 | jffs_init_1pass_list(part); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1502 | pL = (struct b_lists *)part->jffs2_priv; |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1503 | buf = malloc(DEFAULT_EMPTY_SCAN_SIZE); |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1504 | puts ("Scanning JFFS2 FS: "); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1505 | |
| 1506 | /* start at the beginning of the partition */ |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1507 | for (i = 0; i < nr_sectors; i++) { |
| 1508 | uint32_t sector_ofs = i * part->sector_size; |
| 1509 | uint32_t buf_ofs = sector_ofs; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1510 | uint32_t buf_len; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1511 | uint32_t ofs, prevofs; |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1512 | #ifdef CONFIG_JFFS2_SUMMARY |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1513 | struct jffs2_sum_marker *sm; |
| 1514 | void *sumptr = NULL; |
| 1515 | uint32_t sumlen; |
| 1516 | int ret; |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1517 | #endif |
Mark Tomlinson | 56a7f48 | 2015-07-01 16:38:28 +1200 | [diff] [blame] | 1518 | /* Indicates a sector with a CLEANMARKER was found */ |
| 1519 | int clean_sector = 0; |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1520 | struct jffs2_unknown_node crcnode; |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1521 | struct b_node *b; |
wdenk | 7ad5e4c | 2004-04-25 15:41:35 +0000 | [diff] [blame] | 1522 | |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1523 | /* Set buf_size to maximum length */ |
| 1524 | buf_size = DEFAULT_EMPTY_SCAN_SIZE; |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 1525 | schedule(); |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1526 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1527 | #ifdef CONFIG_JFFS2_SUMMARY |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1528 | 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 Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1548 | free(buf); |
| 1549 | jffs2_free_cache(part); |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1550 | 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 Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1571 | if (ret < 0) { |
| 1572 | free(buf); |
| 1573 | jffs2_free_cache(part); |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1574 | return 0; |
Ilya Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1575 | } |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1576 | if (ret) |
| 1577 | continue; |
| 1578 | |
| 1579 | } |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1580 | #endif /* CONFIG_JFFS2_SUMMARY */ |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1581 | |
| 1582 | buf_len = EMPTY_SCAN_SIZE(part->sector_size); |
| 1583 | |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1584 | get_fl_mem((u32)part->offset + buf_ofs, buf_len, buf); |
Stuart Wood | 471a2f0 | 2008-06-02 16:40:08 -0400 | [diff] [blame] | 1585 | |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1586 | /* 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 Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1599 | /* |
| 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 Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1604 | |
| 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 Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1615 | ofs + sizeof(struct jffs2_unknown_node)) |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1616 | break; |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1617 | if (buf_ofs + buf_len < |
| 1618 | ofs + sizeof(struct jffs2_unknown_node)) { |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1619 | 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 Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1626 | node = (union jffs2_node_union *)&buf[ofs - buf_ofs]; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1627 | |
| 1628 | if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) { |
| 1629 | uint32_t inbuf_ofs; |
Wolfgang Denk | bf913c1 | 2011-10-05 22:58:11 +0200 | [diff] [blame] | 1630 | uint32_t scan_end; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1631 | |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1632 | 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 Tomlinson | 56a7f48 | 2015-07-01 16:38:28 +1200 | [diff] [blame] | 1647 | /* |
| 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 Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1655 | |
| 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 Tomlinson | 56a7f48 | 2015-07-01 16:38:28 +1200 | [diff] [blame] | 1676 | /* |
| 1677 | * Found something not erased in the sector, so reset |
| 1678 | * the 'clean_sector' flag. |
| 1679 | */ |
| 1680 | clean_sector = 0; |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1681 | if (node->u.magic != JFFS2_MAGIC_BITMASK) { |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1682 | ofs += 4; |
| 1683 | counter4++; |
| 1684 | continue; |
| 1685 | } |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1686 | |
| 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 Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1698 | ofs += 4; |
| 1699 | counter4++; |
| 1700 | continue; |
| 1701 | } |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1702 | |
| 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 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1711 | /* if its a fragment add it */ |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1712 | switch (node->u.nodetype) { |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1713 | case JFFS2_NODETYPE_INODE: |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1714 | if (buf_ofs + buf_len < |
| 1715 | ofs + sizeof(struct jffs2_raw_inode)) { |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1716 | buf_len = min_t(uint32_t, |
| 1717 | sizeof(struct jffs2_raw_inode), |
| 1718 | sector_ofs + |
| 1719 | part->sector_size - |
| 1720 | ofs); |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1721 | get_fl_mem((u32)part->offset + ofs, |
| 1722 | buf_len, buf); |
| 1723 | buf_ofs = ofs; |
| 1724 | node = (void *)buf; |
| 1725 | } |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1726 | if (!inode_crc((struct jffs2_raw_inode *)node)) |
| 1727 | break; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1728 | |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1729 | b = insert_node(&pL->frag); |
| 1730 | if (!b) { |
Ilya Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1731 | free(buf); |
| 1732 | jffs2_free_cache(part); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1733 | return 0; |
Ilya Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1734 | } |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1735 | b->offset = (u32)part->offset + ofs; |
| 1736 | b->version = node->i.version; |
| 1737 | b->ino = node->i.ino; |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1738 | if (max_totlen < node->u.totlen) |
| 1739 | max_totlen = node->u.totlen; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1740 | 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 Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1747 | buf_len = min_t(uint32_t, |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1748 | node->u.totlen, |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1749 | sector_ofs + |
| 1750 | part->sector_size - |
| 1751 | ofs); |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1752 | get_fl_mem((u32)part->offset + ofs, |
| 1753 | buf_len, buf); |
| 1754 | buf_ofs = ofs; |
| 1755 | node = (void *)buf; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1756 | } |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1757 | |
| 1758 | if (!dirent_crc((struct jffs2_raw_dirent *) |
| 1759 | node) || |
| 1760 | !dirent_name_crc( |
| 1761 | (struct |
| 1762 | jffs2_raw_dirent *) |
| 1763 | node)) |
| 1764 | break; |
wdenk | 7ad5e4c | 2004-04-25 15:41:35 +0000 | [diff] [blame] | 1765 | if (! (counterN%100)) |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1766 | puts ("\b\b. "); |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1767 | b = insert_node(&pL->dir); |
| 1768 | if (!b) { |
Ilya Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1769 | free(buf); |
| 1770 | jffs2_free_cache(part); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1771 | return 0; |
Ilya Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1772 | } |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1773 | b->offset = (u32)part->offset + ofs; |
| 1774 | b->version = node->d.version; |
| 1775 | b->pino = node->d.pino; |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1776 | if (max_totlen < node->u.totlen) |
| 1777 | max_totlen = node->u.totlen; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1778 | counterN++; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1779 | break; |
| 1780 | case JFFS2_NODETYPE_CLEANMARKER: |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1781 | if (node->u.totlen != sizeof(struct jffs2_unknown_node)) |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1782 | printf("OOPS Cleanmarker has bad size " |
Wolfgang Denk | 509cd07 | 2008-07-14 15:06:35 +0200 | [diff] [blame] | 1783 | "%d != %zu\n", |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1784 | node->u.totlen, |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1785 | sizeof(struct jffs2_unknown_node)); |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1786 | if (node->u.totlen == |
| 1787 | sizeof(struct jffs2_unknown_node) && |
| 1788 | ofs == sector_ofs) { |
Mark Tomlinson | 56a7f48 | 2015-07-01 16:38:28 +1200 | [diff] [blame] | 1789 | /* |
| 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 Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1796 | break; |
| 1797 | case JFFS2_NODETYPE_PADDING: |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1798 | if (node->u.totlen < |
| 1799 | sizeof(struct jffs2_unknown_node)) |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1800 | printf("OOPS Padding has bad size " |
Wolfgang Denk | 509cd07 | 2008-07-14 15:06:35 +0200 | [diff] [blame] | 1801 | "%d < %zu\n", |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1802 | node->u.totlen, |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1803 | sizeof(struct jffs2_unknown_node)); |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1804 | break; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1805 | case JFFS2_NODETYPE_SUMMARY: |
| 1806 | break; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1807 | default: |
Wolfgang Denk | 509cd07 | 2008-07-14 15:06:35 +0200 | [diff] [blame] | 1808 | printf("Unknown node type: %x len %d offset 0x%x\n", |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1809 | node->u.nodetype, |
| 1810 | node->u.totlen, ofs); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1811 | } |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1812 | ofs += ((node->u.totlen + 3) & ~3); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1813 | counterF++; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1814 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1817 | free(buf); |
Mark Tomlinson | efdc2a6 | 2015-07-01 16:38:29 +1200 | [diff] [blame] | 1818 | #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 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1825 | putstr("\b\b done.\r\n"); /* close off the dots */ |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1826 | |
| 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 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1833 | /* turn the lcd back on. */ |
| 1834 | /* splash(); */ |
| 1835 | |
| 1836 | #if 0 |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1837 | putLabeledWord("dir entries = ", pL->dir.listCount); |
| 1838 | putLabeledWord("frag entries = ", pL->frag.listCount); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1839 | putLabeledWord("+4 increments = ", counter4); |
| 1840 | putLabeledWord("+file_offset increments = ", counterF); |
| 1841 | |
| 1842 | #endif |
| 1843 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1844 | #ifdef DEBUG_DIRENTS |
| 1845 | dump_dirents(pL); |
| 1846 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1847 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1848 | #ifdef DEBUG_FRAGMENTS |
| 1849 | dump_fragments(pL); |
| 1850 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1851 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1852 | /* 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 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1858 | static u32 |
| 1859 | jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL) |
| 1860 | { |
| 1861 | struct b_node *b; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1862 | struct jffs2_raw_inode ojNode; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1863 | struct jffs2_raw_inode *jNode; |
| 1864 | int i; |
| 1865 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1866 | 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 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1872 | b = pL->frag.listHead; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1873 | while (b) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1874 | jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, |
| 1875 | sizeof(ojNode), &ojNode); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1876 | 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 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1887 | static struct b_lists * |
| 1888 | jffs2_get_list(struct part_info * part, const char *who) |
| 1889 | { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 1890 | /* copy requested part_info struct pointer to global location */ |
| 1891 | current_part = part; |
| 1892 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1893 | 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 */ |
| 1904 | u32 |
| 1905 | jffs2_1pass_ls(struct part_info * part, const char *fname) |
| 1906 | { |
| 1907 | struct b_lists *pl; |
Wolfgang Denk | 79117cd | 2005-08-16 09:32:45 +0200 | [diff] [blame] | 1908 | long ret = 1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1909 | u32 inode; |
| 1910 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1911 | if (! (pl = jffs2_get_list(part, "ls"))) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1912 | 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 | } |
wdenk | 7ad5e4c | 2004-04-25 15:41:35 +0000 | [diff] [blame] | 1918 | |
| 1919 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1920 | #if 0 |
| 1921 | putLabeledWord("found file at inode = ", inode); |
| 1922 | putLabeledWord("read_inode returns = ", ret); |
| 1923 | #endif |
wdenk | 7ad5e4c | 2004-04-25 15:41:35 +0000 | [diff] [blame] | 1924 | |
| 1925 | return ret; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1929 | /* Load a file from flash into memory. fname can be a full path */ |
| 1930 | u32 |
| 1931 | jffs2_1pass_load(char *dest, struct part_info * part, const char *fname) |
| 1932 | { |
| 1933 | |
| 1934 | struct b_lists *pl; |
Wolfgang Denk | 79117cd | 2005-08-16 09:32:45 +0200 | [diff] [blame] | 1935 | long ret = 1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1936 | 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 */ |
| 1963 | u32 |
| 1964 | jffs2_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); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1974 | for (i = 0; i < JFFS2_NUM_COMPR; i++) { |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1975 | 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); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1983 | } |
| 1984 | return 1; |
| 1985 | } |