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 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 113 | #include <config.h> |
| 114 | #include <malloc.h> |
Tom Rini | 3cd84d6 | 2013-12-05 14:48:38 -0500 | [diff] [blame] | 115 | #include <div64.h> |
Tom Rini | 7c4734d | 2016-03-27 14:48:36 -0400 | [diff] [blame] | 116 | #include <linux/compiler.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 117 | #include <linux/stat.h> |
| 118 | #include <linux/time.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 119 | #include <u-boot/crc.h> |
Stuart Wood | 471a2f0 | 2008-06-02 16:40:08 -0400 | [diff] [blame] | 120 | #include <watchdog.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 121 | #include <jffs2/jffs2.h> |
| 122 | #include <jffs2/jffs2_1pass.h> |
Mike Frysinger | 11d1a09 | 2012-04-09 13:39:55 +0000 | [diff] [blame] | 123 | #include <linux/compat.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 124 | #include <linux/errno.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 125 | |
| 126 | #include "jffs2_private.h" |
| 127 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 128 | #define NODE_CHUNK 1024 /* size of memory allocation chunk in b_nodes */ |
| 129 | #define SPIN_BLKSIZE 18 /* spin after having scanned 1<<BLKSIZE bytes */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 130 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 131 | /* Debugging switches */ |
| 132 | #undef DEBUG_DIRENTS /* print directory entry list after scan */ |
| 133 | #undef DEBUG_FRAGMENTS /* print fragment list after scan */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 134 | #undef DEBUG /* enable debugging messages */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 135 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 136 | #ifdef DEBUG |
| 137 | # define DEBUGF(fmt,args...) printf(fmt ,##args) |
| 138 | #else |
| 139 | # define DEBUGF(fmt,args...) |
| 140 | #endif |
| 141 | |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 142 | #include "summary.h" |
| 143 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 144 | /* keeps pointer to currentlu processed partition */ |
| 145 | static struct part_info *current_part; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 146 | |
Wolfgang Denk | 15888b4 | 2007-07-05 17:56:27 +0200 | [diff] [blame] | 147 | #if (defined(CONFIG_JFFS2_NAND) && \ |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 148 | defined(CONFIG_CMD_NAND) ) |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 149 | #include <nand.h> |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 150 | /* |
| 151 | * Support for jffs2 on top of NAND-flash |
| 152 | * |
| 153 | * NAND memory isn't mapped in processor's address space, |
| 154 | * so data should be fetched from flash before |
| 155 | * being processed. This is exactly what functions declared |
| 156 | * here do. |
| 157 | * |
| 158 | */ |
| 159 | |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 160 | #define NAND_PAGE_SIZE 512 |
| 161 | #define NAND_PAGE_SHIFT 9 |
| 162 | #define NAND_PAGE_MASK (~(NAND_PAGE_SIZE-1)) |
| 163 | |
| 164 | #ifndef NAND_CACHE_PAGES |
| 165 | #define NAND_CACHE_PAGES 16 |
| 166 | #endif |
| 167 | #define NAND_CACHE_SIZE (NAND_CACHE_PAGES*NAND_PAGE_SIZE) |
| 168 | |
| 169 | static u8* nand_cache = NULL; |
| 170 | static u32 nand_cache_off = (u32)-1; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 171 | |
| 172 | static int read_nand_cached(u32 off, u32 size, u_char *buf) |
| 173 | { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 174 | struct mtdids *id = current_part->dev->id; |
Grygorii Strashko | ac8f8e3 | 2017-06-26 19:12:57 -0500 | [diff] [blame] | 175 | struct mtd_info *mtd; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 176 | u32 bytes_read = 0; |
Marian Balakowicz | 6a07675 | 2006-04-08 19:08:06 +0200 | [diff] [blame] | 177 | size_t retlen; |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 178 | size_t toread; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 179 | int cpy_bytes; |
| 180 | |
Grygorii Strashko | ac8f8e3 | 2017-06-26 19:12:57 -0500 | [diff] [blame] | 181 | mtd = get_nand_dev_by_index(id->num); |
| 182 | if (!mtd) |
| 183 | return -1; |
| 184 | |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 185 | while (bytes_read < size) { |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 186 | retlen = NAND_CACHE_SIZE; |
| 187 | if( nand_cache_off + retlen > mtd->size ) |
| 188 | retlen = mtd->size - nand_cache_off; |
| 189 | |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 190 | if ((off + bytes_read < nand_cache_off) || |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 191 | (off + bytes_read >= nand_cache_off + retlen)) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 192 | nand_cache_off = (off + bytes_read) & NAND_PAGE_MASK; |
| 193 | if (!nand_cache) { |
| 194 | /* This memory never gets freed but 'cause |
| 195 | it's a bootloader, nobody cares */ |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 196 | nand_cache = malloc(NAND_CACHE_SIZE); |
| 197 | if (!nand_cache) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 198 | printf("read_nand_cached: can't alloc cache size %d bytes\n", |
| 199 | NAND_CACHE_SIZE); |
| 200 | return -1; |
| 201 | } |
| 202 | } |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 203 | |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 204 | toread = NAND_CACHE_SIZE; |
| 205 | if( nand_cache_off + toread > mtd->size ) |
| 206 | toread = mtd->size - nand_cache_off; |
| 207 | |
| 208 | retlen = toread; |
Grygorii Strashko | ac8f8e3 | 2017-06-26 19:12:57 -0500 | [diff] [blame] | 209 | if (nand_read(mtd, nand_cache_off, |
Engling, Uwe | e5e23ee | 2017-10-10 14:20:55 +0000 | [diff] [blame] | 210 | &retlen, nand_cache) < 0 || |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 211 | retlen != toread) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 212 | 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] | 213 | nand_cache_off, toread); |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 214 | return -1; |
| 215 | } |
| 216 | } |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 217 | cpy_bytes = nand_cache_off + retlen - (off + bytes_read); |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 218 | if (cpy_bytes > size - bytes_read) |
| 219 | cpy_bytes = size - bytes_read; |
| 220 | memcpy(buf + bytes_read, |
| 221 | nand_cache + off + bytes_read - nand_cache_off, |
| 222 | cpy_bytes); |
| 223 | bytes_read += cpy_bytes; |
| 224 | } |
| 225 | return bytes_read; |
| 226 | } |
| 227 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 228 | static void *get_fl_mem_nand(u32 off, u32 size, void *ext_buf) |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 229 | { |
| 230 | u_char *buf = ext_buf ? (u_char*)ext_buf : (u_char*)malloc(size); |
| 231 | |
| 232 | if (NULL == buf) { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 233 | printf("get_fl_mem_nand: can't alloc %d bytes\n", size); |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 234 | return NULL; |
| 235 | } |
| 236 | if (read_nand_cached(off, size, buf) < 0) { |
wdenk | f248ebc | 2004-05-05 19:44:41 +0000 | [diff] [blame] | 237 | if (!ext_buf) |
| 238 | free(buf); |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | return buf; |
| 243 | } |
| 244 | |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 245 | static void *get_node_mem_nand(u32 off, void *ext_buf) |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 246 | { |
| 247 | struct jffs2_unknown_node node; |
| 248 | void *ret = NULL; |
| 249 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 250 | if (NULL == get_fl_mem_nand(off, sizeof(node), &node)) |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 251 | return NULL; |
| 252 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 253 | if (!(ret = get_fl_mem_nand(off, node.magic == |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 254 | JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node), |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 255 | ext_buf))) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 256 | printf("off = %#x magic %#x type %#x node.totlen = %d\n", |
| 257 | off, node.magic, node.nodetype, node.totlen); |
| 258 | } |
| 259 | return ret; |
| 260 | } |
| 261 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 262 | static void put_fl_mem_nand(void *buf) |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 263 | { |
| 264 | free(buf); |
| 265 | } |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 266 | #endif |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 267 | |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 268 | #if defined(CONFIG_CMD_ONENAND) |
| 269 | |
| 270 | #include <linux/mtd/mtd.h> |
| 271 | #include <linux/mtd/onenand.h> |
| 272 | #include <onenand_uboot.h> |
| 273 | |
| 274 | #define ONENAND_PAGE_SIZE 2048 |
| 275 | #define ONENAND_PAGE_SHIFT 11 |
| 276 | #define ONENAND_PAGE_MASK (~(ONENAND_PAGE_SIZE-1)) |
| 277 | |
| 278 | #ifndef ONENAND_CACHE_PAGES |
| 279 | #define ONENAND_CACHE_PAGES 4 |
| 280 | #endif |
| 281 | #define ONENAND_CACHE_SIZE (ONENAND_CACHE_PAGES*ONENAND_PAGE_SIZE) |
| 282 | |
| 283 | static u8* onenand_cache; |
| 284 | static u32 onenand_cache_off = (u32)-1; |
| 285 | |
| 286 | static int read_onenand_cached(u32 off, u32 size, u_char *buf) |
| 287 | { |
| 288 | u32 bytes_read = 0; |
| 289 | size_t retlen; |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 290 | size_t toread; |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 291 | int cpy_bytes; |
| 292 | |
| 293 | while (bytes_read < size) { |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 294 | retlen = ONENAND_CACHE_SIZE; |
| 295 | if( onenand_cache_off + retlen > onenand_mtd.size ) |
| 296 | retlen = onenand_mtd.size - onenand_cache_off; |
| 297 | |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 298 | if ((off + bytes_read < onenand_cache_off) || |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 299 | (off + bytes_read >= onenand_cache_off + retlen)) { |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 300 | onenand_cache_off = (off + bytes_read) & ONENAND_PAGE_MASK; |
| 301 | if (!onenand_cache) { |
| 302 | /* This memory never gets freed but 'cause |
| 303 | it's a bootloader, nobody cares */ |
| 304 | onenand_cache = malloc(ONENAND_CACHE_SIZE); |
| 305 | if (!onenand_cache) { |
| 306 | printf("read_onenand_cached: can't alloc cache size %d bytes\n", |
| 307 | ONENAND_CACHE_SIZE); |
| 308 | return -1; |
| 309 | } |
| 310 | } |
| 311 | |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 312 | toread = ONENAND_CACHE_SIZE; |
| 313 | if( onenand_cache_off + toread > onenand_mtd.size ) |
| 314 | toread = onenand_mtd.size - onenand_cache_off; |
| 315 | retlen = toread; |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 316 | if (onenand_read(&onenand_mtd, onenand_cache_off, retlen, |
Engling, Uwe | e5e23ee | 2017-10-10 14:20:55 +0000 | [diff] [blame] | 317 | &retlen, onenand_cache) < 0 || |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 318 | retlen != toread) { |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 319 | 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] | 320 | onenand_cache_off, toread); |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 321 | return -1; |
| 322 | } |
| 323 | } |
Wagner Popov dos Santos | a943bef | 2021-02-22 23:30:58 -0300 | [diff] [blame] | 324 | cpy_bytes = onenand_cache_off + retlen - (off + bytes_read); |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 325 | if (cpy_bytes > size - bytes_read) |
| 326 | cpy_bytes = size - bytes_read; |
| 327 | memcpy(buf + bytes_read, |
| 328 | onenand_cache + off + bytes_read - onenand_cache_off, |
| 329 | cpy_bytes); |
| 330 | bytes_read += cpy_bytes; |
| 331 | } |
| 332 | return bytes_read; |
| 333 | } |
| 334 | |
| 335 | static void *get_fl_mem_onenand(u32 off, u32 size, void *ext_buf) |
| 336 | { |
| 337 | u_char *buf = ext_buf ? (u_char *)ext_buf : (u_char *)malloc(size); |
| 338 | |
| 339 | if (NULL == buf) { |
| 340 | printf("get_fl_mem_onenand: can't alloc %d bytes\n", size); |
| 341 | return NULL; |
| 342 | } |
| 343 | if (read_onenand_cached(off, size, buf) < 0) { |
| 344 | if (!ext_buf) |
| 345 | free(buf); |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | return buf; |
| 350 | } |
| 351 | |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 352 | static void *get_node_mem_onenand(u32 off, void *ext_buf) |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 353 | { |
| 354 | struct jffs2_unknown_node node; |
| 355 | void *ret = NULL; |
| 356 | |
| 357 | if (NULL == get_fl_mem_onenand(off, sizeof(node), &node)) |
| 358 | return NULL; |
| 359 | |
| 360 | ret = get_fl_mem_onenand(off, node.magic == |
| 361 | JFFS2_MAGIC_BITMASK ? node.totlen : sizeof(node), |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 362 | ext_buf); |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 363 | if (!ret) { |
| 364 | printf("off = %#x magic %#x type %#x node.totlen = %d\n", |
| 365 | off, node.magic, node.nodetype, node.totlen); |
| 366 | } |
| 367 | return ret; |
| 368 | } |
| 369 | |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 370 | static void put_fl_mem_onenand(void *buf) |
| 371 | { |
| 372 | free(buf); |
| 373 | } |
| 374 | #endif |
| 375 | |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 376 | #if defined(CONFIG_CMD_FLASH) |
Tom Rini | 063c938 | 2022-07-23 13:05:03 -0400 | [diff] [blame] | 377 | #include <flash.h> |
| 378 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 379 | /* |
| 380 | * Support for jffs2 on top of NOR-flash |
| 381 | * |
| 382 | * NOR flash memory is mapped in processor's address space, |
| 383 | * just return address. |
| 384 | */ |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 385 | 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] | 386 | { |
| 387 | u32 addr = off; |
| 388 | struct mtdids *id = current_part->dev->id; |
| 389 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 390 | flash_info_t *flash = &flash_info[id->num]; |
| 391 | |
| 392 | addr += flash->start[0]; |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 393 | if (ext_buf) { |
| 394 | memcpy(ext_buf, (void *)addr, size); |
| 395 | return ext_buf; |
| 396 | } |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 397 | return (void*)addr; |
| 398 | } |
| 399 | |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 400 | static inline void *get_node_mem_nor(u32 off, void *ext_buf) |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 401 | { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 402 | struct jffs2_unknown_node *pNode; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 403 | |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 404 | /* pNode will point directly to flash - don't provide external buffer |
| 405 | and don't care about size */ |
| 406 | pNode = get_fl_mem_nor(off, 0, NULL); |
| 407 | return (void *)get_fl_mem_nor(off, pNode->magic == JFFS2_MAGIC_BITMASK ? |
| 408 | pNode->totlen : sizeof(*pNode), ext_buf); |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 409 | } |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 410 | #endif |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 411 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 412 | /* |
Wolfgang Denk | eb95c85 | 2005-08-10 15:14:32 +0200 | [diff] [blame] | 413 | * Generic jffs2 raw memory and node read routines. |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 414 | * |
| 415 | */ |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 416 | static inline void *get_fl_mem(u32 off, u32 size, void *ext_buf) |
| 417 | { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 418 | struct mtdids *id = current_part->dev->id; |
Wolfgang Denk | eb95c85 | 2005-08-10 15:14:32 +0200 | [diff] [blame] | 419 | |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 420 | switch(id->type) { |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 421 | #if defined(CONFIG_CMD_FLASH) |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 422 | case MTD_DEV_TYPE_NOR: |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 423 | return get_fl_mem_nor(off, size, ext_buf); |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 424 | break; |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 425 | #endif |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 426 | #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND) |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 427 | case MTD_DEV_TYPE_NAND: |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 428 | return get_fl_mem_nand(off, size, ext_buf); |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 429 | break; |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 430 | #endif |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 431 | #if defined(CONFIG_CMD_ONENAND) |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 432 | case MTD_DEV_TYPE_ONENAND: |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 433 | return get_fl_mem_onenand(off, size, ext_buf); |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 434 | break; |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 435 | #endif |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 436 | default: |
| 437 | printf("get_fl_mem: unknown device type, " \ |
| 438 | "using raw offset!\n"); |
| 439 | } |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 440 | return (void*)off; |
| 441 | } |
| 442 | |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 443 | static inline void *get_node_mem(u32 off, void *ext_buf) |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 444 | { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 445 | struct mtdids *id = current_part->dev->id; |
Wolfgang Denk | eb95c85 | 2005-08-10 15:14:32 +0200 | [diff] [blame] | 446 | |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 447 | switch(id->type) { |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 448 | #if defined(CONFIG_CMD_FLASH) |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 449 | case MTD_DEV_TYPE_NOR: |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 450 | return get_node_mem_nor(off, ext_buf); |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 451 | break; |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 452 | #endif |
Wolfgang Denk | 15888b4 | 2007-07-05 17:56:27 +0200 | [diff] [blame] | 453 | #if defined(CONFIG_JFFS2_NAND) && \ |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 454 | defined(CONFIG_CMD_NAND) |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 455 | case MTD_DEV_TYPE_NAND: |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 456 | return get_node_mem_nand(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 |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 459 | #if defined(CONFIG_CMD_ONENAND) |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 460 | case MTD_DEV_TYPE_ONENAND: |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 461 | return get_node_mem_onenand(off, ext_buf); |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 462 | break; |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 463 | #endif |
Heiko Schocher | d64a813 | 2010-03-24 13:22:50 +0100 | [diff] [blame] | 464 | default: |
| 465 | printf("get_fl_mem: unknown device type, " \ |
| 466 | "using raw offset!\n"); |
| 467 | } |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 468 | return (void*)off; |
| 469 | } |
| 470 | |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 471 | static inline void put_fl_mem(void *buf, void *ext_buf) |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 472 | { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 473 | struct mtdids *id = current_part->dev->id; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 474 | |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 475 | /* If buf is the same as ext_buf, it was provided by the caller - |
| 476 | we shouldn't free it then. */ |
| 477 | if (buf == ext_buf) |
| 478 | return; |
Scott Wood | c97ba11 | 2008-10-31 13:51:12 -0500 | [diff] [blame] | 479 | switch (id->type) { |
| 480 | #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND) |
| 481 | case MTD_DEV_TYPE_NAND: |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 482 | return put_fl_mem_nand(buf); |
| 483 | #endif |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 484 | #if defined(CONFIG_CMD_ONENAND) |
Scott Wood | c97ba11 | 2008-10-31 13:51:12 -0500 | [diff] [blame] | 485 | case MTD_DEV_TYPE_ONENAND: |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 486 | return put_fl_mem_onenand(buf); |
| 487 | #endif |
Scott Wood | c97ba11 | 2008-10-31 13:51:12 -0500 | [diff] [blame] | 488 | } |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 489 | } |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 490 | |
| 491 | /* Compression names */ |
| 492 | static char *compr_names[] = { |
| 493 | "NONE", |
| 494 | "ZERO", |
| 495 | "RTIME", |
| 496 | "RUBINMIPS", |
| 497 | "COPY", |
| 498 | "DYNRUBIN", |
wdenk | e84ec90 | 2005-05-05 00:04:14 +0000 | [diff] [blame] | 499 | "ZLIB", |
Wolfgang Denk | 0eded4f | 2010-01-15 11:10:33 +0100 | [diff] [blame] | 500 | #if defined(CONFIG_JFFS2_LZO) |
wdenk | e84ec90 | 2005-05-05 00:04:14 +0000 | [diff] [blame] | 501 | "LZO", |
wdenk | e84ec90 | 2005-05-05 00:04:14 +0000 | [diff] [blame] | 502 | #endif |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 503 | }; |
| 504 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 505 | /* Memory management */ |
| 506 | struct mem_block { |
| 507 | u32 index; |
| 508 | struct mem_block *next; |
| 509 | struct b_node nodes[NODE_CHUNK]; |
| 510 | }; |
| 511 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 512 | static void |
| 513 | free_nodes(struct b_list *list) |
| 514 | { |
| 515 | while (list->listMemBase != NULL) { |
| 516 | struct mem_block *next = list->listMemBase->next; |
| 517 | free( list->listMemBase ); |
| 518 | list->listMemBase = next; |
| 519 | } |
| 520 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 521 | |
| 522 | static struct b_node * |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 523 | add_node(struct b_list *list) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 524 | { |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 525 | u32 index = 0; |
| 526 | struct mem_block *memBase; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 527 | struct b_node *b; |
| 528 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 529 | memBase = list->listMemBase; |
| 530 | if (memBase != NULL) |
| 531 | index = memBase->index; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 532 | #if 0 |
| 533 | putLabeledWord("add_node: index = ", index); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 534 | putLabeledWord("add_node: memBase = ", list->listMemBase); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 535 | #endif |
| 536 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 537 | if (memBase == NULL || index >= NODE_CHUNK) { |
| 538 | /* we need more space before we continue */ |
| 539 | memBase = mmalloc(sizeof(struct mem_block)); |
| 540 | if (memBase == NULL) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 541 | putstr("add_node: malloc failed\n"); |
| 542 | return NULL; |
| 543 | } |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 544 | memBase->next = list->listMemBase; |
| 545 | index = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 546 | #if 0 |
| 547 | putLabeledWord("add_node: alloced a new membase at ", *memBase); |
| 548 | #endif |
| 549 | |
| 550 | } |
| 551 | /* now we have room to add it. */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 552 | b = &memBase->nodes[index]; |
| 553 | index ++; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 554 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 555 | memBase->index = index; |
| 556 | list->listMemBase = memBase; |
| 557 | list->listCount++; |
| 558 | return b; |
| 559 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 560 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 561 | static struct b_node * |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 562 | insert_node(struct b_list *list) |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 563 | { |
| 564 | struct b_node *new; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 565 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 566 | if (!(new = add_node(list))) { |
| 567 | putstr("add_node failed!\r\n"); |
| 568 | return NULL; |
| 569 | } |
Mark Tomlinson | efdc2a6 | 2015-07-01 16:38:29 +1200 | [diff] [blame] | 570 | new->next = NULL; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 571 | |
Mark Tomlinson | efdc2a6 | 2015-07-01 16:38:29 +1200 | [diff] [blame] | 572 | if (list->listTail != NULL) |
| 573 | list->listTail->next = new; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 574 | else |
Mark Tomlinson | efdc2a6 | 2015-07-01 16:38:29 +1200 | [diff] [blame] | 575 | list->listHead = new; |
| 576 | list->listTail = new; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 577 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 578 | return new; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 581 | #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 582 | /* Sort data entries with the latest version last, so that if there |
| 583 | * is overlapping data the latest version will be used. |
| 584 | */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 585 | static int compare_inodes(struct b_node *new, struct b_node *old) |
| 586 | { |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 587 | return new->version > old->version; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 588 | } |
| 589 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 590 | /* Sort directory entries so all entries in the same directory |
| 591 | * with the same name are grouped together, with the latest version |
| 592 | * last. This makes it easy to eliminate all but the latest version |
| 593 | * by marking the previous version dead by setting the inode to 0. |
| 594 | */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 595 | static int compare_dirents(struct b_node *new, struct b_node *old) |
| 596 | { |
Mark Tomlinson | 7c6558e | 2015-07-01 16:38:23 +1200 | [diff] [blame] | 597 | /* |
| 598 | * Using NULL as the buffer for NOR flash prevents the entire node |
| 599 | * being read. This makes most comparisons much quicker as only one |
| 600 | * or two entries from the node will be used most of the time. |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 601 | */ |
Mark Tomlinson | 7c6558e | 2015-07-01 16:38:23 +1200 | [diff] [blame] | 602 | struct jffs2_raw_dirent *jNew = get_node_mem(new->offset, NULL); |
| 603 | struct jffs2_raw_dirent *jOld = get_node_mem(old->offset, NULL); |
| 604 | int cmp; |
| 605 | int ret; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 606 | |
Mark Tomlinson | 7c6558e | 2015-07-01 16:38:23 +1200 | [diff] [blame] | 607 | if (jNew->pino != jOld->pino) { |
| 608 | /* ascending sort by pino */ |
| 609 | ret = jNew->pino > jOld->pino; |
| 610 | } else if (jNew->nsize != jOld->nsize) { |
| 611 | /* |
| 612 | * pino is the same, so use ascending sort by nsize, |
| 613 | * so we don't do strncmp unless we really must. |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 614 | */ |
Mark Tomlinson | 7c6558e | 2015-07-01 16:38:23 +1200 | [diff] [blame] | 615 | ret = jNew->nsize > jOld->nsize; |
| 616 | } else { |
| 617 | /* |
| 618 | * length is also the same, so use ascending sort by name |
| 619 | */ |
| 620 | cmp = strncmp((char *)jNew->name, (char *)jOld->name, |
| 621 | jNew->nsize); |
| 622 | if (cmp != 0) { |
| 623 | ret = cmp > 0; |
| 624 | } else { |
| 625 | /* |
| 626 | * we have duplicate names in this directory, |
| 627 | * so use ascending sort by version |
| 628 | */ |
| 629 | ret = jNew->version > jOld->version; |
| 630 | } |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 631 | } |
Mark Tomlinson | 7c6558e | 2015-07-01 16:38:23 +1200 | [diff] [blame] | 632 | put_fl_mem(jNew, NULL); |
| 633 | put_fl_mem(jOld, NULL); |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 634 | |
Mark Tomlinson | 7c6558e | 2015-07-01 16:38:23 +1200 | [diff] [blame] | 635 | return ret; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 636 | } |
| 637 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 638 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 639 | void |
| 640 | jffs2_free_cache(struct part_info *part) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 641 | { |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 642 | struct b_lists *pL; |
| 643 | |
| 644 | if (part->jffs2_priv != NULL) { |
| 645 | pL = (struct b_lists *)part->jffs2_priv; |
| 646 | free_nodes(&pL->frag); |
| 647 | free_nodes(&pL->dir); |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 648 | free(pL->readbuf); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 649 | free(pL); |
| 650 | } |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static u32 |
| 654 | jffs_init_1pass_list(struct part_info *part) |
| 655 | { |
| 656 | struct b_lists *pL; |
| 657 | |
| 658 | jffs2_free_cache(part); |
| 659 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 660 | if (NULL != (part->jffs2_priv = malloc(sizeof(struct b_lists)))) { |
| 661 | pL = (struct b_lists *)part->jffs2_priv; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 662 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 663 | memset(pL, 0, sizeof(*pL)); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 664 | #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 665 | pL->dir.listCompare = compare_dirents; |
| 666 | pL->frag.listCompare = compare_inodes; |
| 667 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 668 | } |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | /* find the inode from the slashless name given a parent */ |
| 673 | static long |
| 674 | jffs2_1pass_read_inode(struct b_lists *pL, u32 inode, char *dest) |
| 675 | { |
| 676 | struct b_node *b; |
| 677 | struct jffs2_raw_inode *jNode; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 678 | u32 totalSize = 0; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 679 | u32 latestVersion = 0; |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 680 | uchar *lDest; |
| 681 | uchar *src; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 682 | int i; |
| 683 | u32 counter = 0; |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 684 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 685 | /* Find file size before loading any data, so fragments that |
| 686 | * start past the end of file can be ignored. A fragment |
| 687 | * that is partially in the file is loaded, so extra data may |
| 688 | * be loaded up to the next 4K boundary above the file size. |
| 689 | * This shouldn't cause trouble when loading kernel images, so |
| 690 | * we will live with it. |
| 691 | */ |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 692 | int latestOffset = -1; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 693 | for (b = pL->frag.listHead; b != NULL; b = b->next) { |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 694 | if (inode == b->ino) { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 695 | /* get actual file length from the newest node */ |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 696 | if (b->version >= latestVersion) { |
| 697 | latestVersion = b->version; |
| 698 | latestOffset = b->offset; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | if (latestOffset >= 0) { |
| 704 | jNode = (struct jffs2_raw_inode *)get_fl_mem(latestOffset, |
| 705 | sizeof(struct jffs2_raw_inode), pL->readbuf); |
| 706 | totalSize = jNode->isize; |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 707 | put_fl_mem(jNode, pL->readbuf); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 708 | } |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 709 | |
Mark Tomlinson | 6d99845 | 2015-07-01 16:38:22 +1200 | [diff] [blame] | 710 | /* |
| 711 | * If no destination is provided, we are done. |
| 712 | * Just return the total size. |
| 713 | */ |
| 714 | if (!dest) |
| 715 | return totalSize; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 716 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 717 | for (b = pL->frag.listHead; b != NULL; b = b->next) { |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 718 | if (inode == b->ino) { |
| 719 | /* |
| 720 | * Copy just the node and not the data at this point, |
| 721 | * since we don't yet know if we need this data. |
| 722 | */ |
| 723 | jNode = (struct jffs2_raw_inode *)get_fl_mem(b->offset, |
| 724 | sizeof(struct jffs2_raw_inode), |
| 725 | pL->readbuf); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 726 | #if 0 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 727 | putLabeledWord("\r\n\r\nread_inode: totlen = ", jNode->totlen); |
| 728 | putLabeledWord("read_inode: inode = ", jNode->ino); |
| 729 | putLabeledWord("read_inode: version = ", jNode->version); |
| 730 | putLabeledWord("read_inode: isize = ", jNode->isize); |
| 731 | putLabeledWord("read_inode: offset = ", jNode->offset); |
| 732 | putLabeledWord("read_inode: csize = ", jNode->csize); |
| 733 | putLabeledWord("read_inode: dsize = ", jNode->dsize); |
| 734 | putLabeledWord("read_inode: compr = ", jNode->compr); |
| 735 | putLabeledWord("read_inode: usercompr = ", jNode->usercompr); |
| 736 | putLabeledWord("read_inode: flags = ", jNode->flags); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 737 | #endif |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 738 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 739 | if(dest) { |
Mark Tomlinson | b71748f | 2015-07-01 16:38:25 +1200 | [diff] [blame] | 740 | /* |
| 741 | * Now that the inode has been checked, |
| 742 | * read the entire inode, including data. |
| 743 | */ |
| 744 | put_fl_mem(jNode, pL->readbuf); |
| 745 | jNode = (struct jffs2_raw_inode *) |
| 746 | get_node_mem(b->offset, pL->readbuf); |
| 747 | src = ((uchar *)jNode) + |
| 748 | sizeof(struct jffs2_raw_inode); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 749 | /* ignore data behind latest known EOF */ |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 750 | if (jNode->offset > totalSize) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 751 | put_fl_mem(jNode, pL->readbuf); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 752 | continue; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 753 | } |
Ilya Yanok | c0c0773 | 2008-11-13 19:49:36 +0300 | [diff] [blame] | 754 | if (b->datacrc == CRC_UNKNOWN) |
| 755 | b->datacrc = data_crc(jNode) ? |
| 756 | CRC_OK : CRC_BAD; |
| 757 | if (b->datacrc == CRC_BAD) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 758 | put_fl_mem(jNode, pL->readbuf); |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 759 | continue; |
| 760 | } |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 761 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 762 | lDest = (uchar *) (dest + jNode->offset); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 763 | #if 0 |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 764 | putLabeledWord("read_inode: src = ", src); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 765 | putLabeledWord("read_inode: dest = ", lDest); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 766 | #endif |
| 767 | switch (jNode->compr) { |
| 768 | case JFFS2_COMPR_NONE: |
Wolfgang Denk | bf913c1 | 2011-10-05 22:58:11 +0200 | [diff] [blame] | 769 | ldr_memcpy(lDest, src, jNode->dsize); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 770 | break; |
| 771 | case JFFS2_COMPR_ZERO: |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 772 | for (i = 0; i < jNode->dsize; i++) |
| 773 | *(lDest++) = 0; |
| 774 | break; |
| 775 | case JFFS2_COMPR_RTIME: |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 776 | rtime_decompress(src, lDest, jNode->csize, jNode->dsize); |
| 777 | break; |
| 778 | case JFFS2_COMPR_DYNRUBIN: |
| 779 | /* this is slow but it works */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 780 | dynrubin_decompress(src, lDest, jNode->csize, jNode->dsize); |
| 781 | break; |
| 782 | case JFFS2_COMPR_ZLIB: |
Wolfgang Denk | bf913c1 | 2011-10-05 22:58:11 +0200 | [diff] [blame] | 783 | zlib_decompress(src, lDest, jNode->csize, jNode->dsize); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 784 | break; |
Wolfgang Denk | 0eded4f | 2010-01-15 11:10:33 +0100 | [diff] [blame] | 785 | #if defined(CONFIG_JFFS2_LZO) |
wdenk | e84ec90 | 2005-05-05 00:04:14 +0000 | [diff] [blame] | 786 | case JFFS2_COMPR_LZO: |
Wolfgang Denk | bf913c1 | 2011-10-05 22:58:11 +0200 | [diff] [blame] | 787 | lzo_decompress(src, lDest, jNode->csize, jNode->dsize); |
wdenk | e84ec90 | 2005-05-05 00:04:14 +0000 | [diff] [blame] | 788 | break; |
| 789 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 790 | default: |
| 791 | /* unknown */ |
Loïc Minier | 5d0569a | 2011-02-03 22:04:26 +0100 | [diff] [blame] | 792 | putLabeledWord("UNKNOWN COMPRESSION METHOD = ", jNode->compr); |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 793 | put_fl_mem(jNode, pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 794 | return -1; |
| 795 | break; |
| 796 | } |
| 797 | } |
| 798 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 799 | #if 0 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 800 | putLabeledWord("read_inode: totalSize = ", totalSize); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 801 | #endif |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 802 | put_fl_mem(jNode, pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 803 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 804 | counter++; |
| 805 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 806 | |
| 807 | #if 0 |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 808 | putLabeledWord("read_inode: returning = ", totalSize); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 809 | #endif |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 810 | return totalSize; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | /* find the inode from the slashless name given a parent */ |
| 814 | static u32 |
| 815 | jffs2_1pass_find_inode(struct b_lists * pL, const char *name, u32 pino) |
| 816 | { |
| 817 | struct b_node *b; |
| 818 | struct jffs2_raw_dirent *jDir; |
| 819 | int len; |
| 820 | u32 counter; |
| 821 | u32 version = 0; |
| 822 | u32 inode = 0; |
| 823 | |
| 824 | /* name is assumed slash free */ |
| 825 | len = strlen(name); |
| 826 | |
| 827 | counter = 0; |
| 828 | /* we need to search all and return the inode with the highest version */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 829 | for(b = pL->dir.listHead; b; b = b->next, counter++) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 830 | jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset, |
| 831 | pL->readbuf); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 832 | if ((pino == jDir->pino) && (len == jDir->nsize) && |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 833 | (!strncmp((char *)jDir->name, name, len))) { /* a match */ |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 834 | if (jDir->version < version) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 835 | put_fl_mem(jDir, pL->readbuf); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 836 | continue; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 837 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 838 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 839 | if (jDir->version == version && inode != 0) { |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 840 | /* I'm pretty sure this isn't legal */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 841 | putstr(" ** ERROR ** "); |
| 842 | putnstr(jDir->name, jDir->nsize); |
| 843 | putLabeledWord(" has dup version =", version); |
| 844 | } |
| 845 | inode = jDir->ino; |
| 846 | version = jDir->version; |
| 847 | } |
| 848 | #if 0 |
| 849 | putstr("\r\nfind_inode:p&l ->"); |
| 850 | putnstr(jDir->name, jDir->nsize); |
| 851 | putstr("\r\n"); |
| 852 | putLabeledWord("pino = ", jDir->pino); |
| 853 | putLabeledWord("nsize = ", jDir->nsize); |
| 854 | putLabeledWord("b = ", (u32) b); |
| 855 | putLabeledWord("counter = ", counter); |
| 856 | #endif |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 857 | put_fl_mem(jDir, pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 858 | } |
| 859 | return inode; |
| 860 | } |
| 861 | |
wdenk | ad276f2 | 2004-01-04 16:28:35 +0000 | [diff] [blame] | 862 | char *mkmodestr(unsigned long mode, char *str) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 863 | { |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 864 | static const char *l = "xwr"; |
| 865 | int mask = 1, i; |
| 866 | char c; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 867 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 868 | switch (mode & S_IFMT) { |
| 869 | case S_IFDIR: str[0] = 'd'; break; |
| 870 | case S_IFBLK: str[0] = 'b'; break; |
| 871 | case S_IFCHR: str[0] = 'c'; break; |
| 872 | case S_IFIFO: str[0] = 'f'; break; |
| 873 | case S_IFLNK: str[0] = 'l'; break; |
| 874 | case S_IFSOCK: str[0] = 's'; break; |
| 875 | case S_IFREG: str[0] = '-'; break; |
| 876 | default: str[0] = '?'; |
| 877 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 878 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 879 | for(i = 0; i < 9; i++) { |
| 880 | c = l[i%3]; |
| 881 | str[9-i] = (mode & mask)?c:'-'; |
| 882 | mask = mask<<1; |
| 883 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 884 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 885 | if(mode & S_ISUID) str[3] = (mode & S_IXUSR)?'s':'S'; |
| 886 | if(mode & S_ISGID) str[6] = (mode & S_IXGRP)?'s':'S'; |
| 887 | if(mode & S_ISVTX) str[9] = (mode & S_IXOTH)?'t':'T'; |
| 888 | str[10] = '\0'; |
| 889 | return str; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static inline void dump_stat(struct stat *st, const char *name) |
| 893 | { |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 894 | char str[20]; |
| 895 | char s[64], *p; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 896 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 897 | if (st->st_mtime == (time_t)(-1)) /* some ctimes really hate -1 */ |
| 898 | st->st_mtime = 1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 899 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 900 | 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] | 901 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 902 | if ((p = strchr(s,'\n')) != NULL) *p = '\0'; |
| 903 | if ((p = strchr(s,'\r')) != NULL) *p = '\0'; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 904 | |
| 905 | /* |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 906 | printf("%6lo %s %8ld %s %s\n", st->st_mode, mkmodestr(st->st_mode, str), |
| 907 | st->st_size, s, name); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 908 | */ |
| 909 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 910 | 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] | 911 | } |
| 912 | |
| 913 | static inline u32 dump_inode(struct b_lists * pL, struct jffs2_raw_dirent *d, struct jffs2_raw_inode *i) |
| 914 | { |
| 915 | char fname[256]; |
| 916 | struct stat st; |
| 917 | |
| 918 | if(!d || !i) return -1; |
| 919 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 920 | strncpy(fname, (char *)d->name, d->nsize); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 921 | fname[d->nsize] = '\0'; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 922 | |
| 923 | memset(&st,0,sizeof(st)); |
| 924 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 925 | st.st_mtime = i->mtime; |
| 926 | st.st_mode = i->mode; |
| 927 | st.st_ino = i->ino; |
Ilya Yanok | c4785fb | 2008-11-13 19:49:31 +0300 | [diff] [blame] | 928 | st.st_size = i->isize; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 929 | |
| 930 | dump_stat(&st, fname); |
| 931 | |
| 932 | if (d->type == DT_LNK) { |
| 933 | unsigned char *src = (unsigned char *) (&i[1]); |
| 934 | putstr(" -> "); |
| 935 | putnstr(src, (int)i->dsize); |
| 936 | } |
| 937 | |
| 938 | putstr("\r\n"); |
| 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | /* list inodes with the given pino */ |
| 944 | static u32 |
| 945 | jffs2_1pass_list_inodes(struct b_lists * pL, u32 pino) |
| 946 | { |
| 947 | struct b_node *b; |
| 948 | struct jffs2_raw_dirent *jDir; |
| 949 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 950 | for (b = pL->dir.listHead; b; b = b->next) { |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 951 | if (pino == b->pino) { |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 952 | u32 i_version = 0; |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 953 | int i_offset = -1; |
| 954 | struct jffs2_raw_inode *jNode = NULL; |
Mark Tomlinson | 2946852 | 2015-07-01 16:38:24 +1200 | [diff] [blame] | 955 | struct b_node *b2; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 956 | |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 957 | jDir = (struct jffs2_raw_dirent *) |
| 958 | get_node_mem(b->offset, pL->readbuf); |
Mark Tomlinson | 2946852 | 2015-07-01 16:38:24 +1200 | [diff] [blame] | 959 | #ifdef CONFIG_SYS_JFFS2_SORT_FRAGMENTS |
| 960 | /* Check for more recent versions of this file */ |
| 961 | int match; |
| 962 | do { |
| 963 | struct b_node *next = b->next; |
| 964 | struct jffs2_raw_dirent *jDirNext; |
| 965 | if (!next) |
| 966 | break; |
| 967 | jDirNext = (struct jffs2_raw_dirent *) |
| 968 | get_node_mem(next->offset, NULL); |
| 969 | match = jDirNext->pino == jDir->pino && |
| 970 | jDirNext->nsize == jDir->nsize && |
| 971 | strncmp((char *)jDirNext->name, |
| 972 | (char *)jDir->name, |
| 973 | jDir->nsize) == 0; |
| 974 | if (match) { |
| 975 | /* Use next. It is more recent */ |
| 976 | b = next; |
| 977 | /* Update buffer with the new info */ |
| 978 | *jDir = *jDirNext; |
| 979 | } |
| 980 | put_fl_mem(jDirNext, NULL); |
| 981 | } while (match); |
| 982 | #endif |
| 983 | if (jDir->ino == 0) { |
| 984 | /* Deleted file */ |
| 985 | put_fl_mem(jDir, pL->readbuf); |
| 986 | continue; |
| 987 | } |
| 988 | |
| 989 | for (b2 = pL->frag.listHead; b2; b2 = b2->next) { |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 990 | if (b2->ino == jDir->ino && |
| 991 | b2->version >= i_version) { |
| 992 | i_version = b2->version; |
| 993 | i_offset = b2->offset; |
wdenk | f248ebc | 2004-05-05 19:44:41 +0000 | [diff] [blame] | 994 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 997 | if (i_version >= 0) { |
| 998 | if (jDir->type == DT_LNK) |
| 999 | jNode = get_node_mem(i_offset, NULL); |
| 1000 | else |
| 1001 | jNode = get_fl_mem(i_offset, |
| 1002 | sizeof(*jNode), |
| 1003 | NULL); |
| 1004 | } |
| 1005 | |
| 1006 | dump_inode(pL, jDir, jNode); |
| 1007 | put_fl_mem(jNode, NULL); |
| 1008 | |
| 1009 | put_fl_mem(jDir, pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | return pino; |
| 1013 | } |
| 1014 | |
| 1015 | static u32 |
| 1016 | jffs2_1pass_search_inode(struct b_lists * pL, const char *fname, u32 pino) |
| 1017 | { |
| 1018 | int i; |
| 1019 | char tmp[256]; |
| 1020 | char working_tmp[256]; |
| 1021 | char *c; |
| 1022 | |
| 1023 | /* discard any leading slash */ |
| 1024 | i = 0; |
| 1025 | while (fname[i] == '/') |
| 1026 | i++; |
| 1027 | strcpy(tmp, &fname[i]); |
| 1028 | |
| 1029 | while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */ |
| 1030 | { |
| 1031 | strncpy(working_tmp, tmp, c - tmp); |
| 1032 | working_tmp[c - tmp] = '\0'; |
| 1033 | #if 0 |
| 1034 | putstr("search_inode: tmp = "); |
| 1035 | putstr(tmp); |
| 1036 | putstr("\r\n"); |
| 1037 | putstr("search_inode: wtmp = "); |
| 1038 | putstr(working_tmp); |
| 1039 | putstr("\r\n"); |
| 1040 | putstr("search_inode: c = "); |
| 1041 | putstr(c); |
| 1042 | putstr("\r\n"); |
| 1043 | #endif |
| 1044 | for (i = 0; i < strlen(c) - 1; i++) |
| 1045 | tmp[i] = c[i + 1]; |
| 1046 | tmp[i] = '\0'; |
| 1047 | #if 0 |
| 1048 | putstr("search_inode: post tmp = "); |
| 1049 | putstr(tmp); |
| 1050 | putstr("\r\n"); |
| 1051 | #endif |
| 1052 | |
| 1053 | if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino))) { |
| 1054 | putstr("find_inode failed for name="); |
| 1055 | putstr(working_tmp); |
| 1056 | putstr("\r\n"); |
| 1057 | return 0; |
| 1058 | } |
| 1059 | } |
| 1060 | /* this is for the bare filename, directories have already been mapped */ |
| 1061 | if (!(pino = jffs2_1pass_find_inode(pL, tmp, pino))) { |
| 1062 | putstr("find_inode failed for name="); |
| 1063 | putstr(tmp); |
| 1064 | putstr("\r\n"); |
| 1065 | return 0; |
| 1066 | } |
| 1067 | return pino; |
| 1068 | |
| 1069 | } |
| 1070 | |
| 1071 | static u32 |
| 1072 | jffs2_1pass_resolve_inode(struct b_lists * pL, u32 ino) |
| 1073 | { |
| 1074 | struct b_node *b; |
| 1075 | struct b_node *b2; |
| 1076 | struct jffs2_raw_dirent *jDir; |
| 1077 | struct jffs2_raw_inode *jNode; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1078 | u8 jDirFoundType = 0; |
| 1079 | u32 jDirFoundIno = 0; |
| 1080 | u32 jDirFoundPino = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1081 | char tmp[256]; |
| 1082 | u32 version = 0; |
| 1083 | u32 pino; |
| 1084 | unsigned char *src; |
| 1085 | |
| 1086 | /* we need to search all and return the inode with the highest version */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1087 | for(b = pL->dir.listHead; b; b = b->next) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1088 | jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset, |
| 1089 | pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1090 | if (ino == jDir->ino) { |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1091 | if (jDir->version < version) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1092 | put_fl_mem(jDir, pL->readbuf); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1093 | continue; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1094 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1095 | |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1096 | if (jDir->version == version && jDirFoundType) { |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1097 | /* I'm pretty sure this isn't legal */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1098 | putstr(" ** ERROR ** "); |
| 1099 | putnstr(jDir->name, jDir->nsize); |
| 1100 | putLabeledWord(" has dup version (resolve) = ", |
| 1101 | version); |
| 1102 | } |
| 1103 | |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1104 | jDirFoundType = jDir->type; |
| 1105 | jDirFoundIno = jDir->ino; |
| 1106 | jDirFoundPino = jDir->pino; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1107 | version = jDir->version; |
| 1108 | } |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1109 | put_fl_mem(jDir, pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1110 | } |
| 1111 | /* now we found the right entry again. (shoulda returned inode*) */ |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1112 | if (jDirFoundType != DT_LNK) |
| 1113 | return jDirFoundIno; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1114 | |
| 1115 | /* it's a soft link so we follow it again. */ |
| 1116 | b2 = pL->frag.listHead; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1117 | while (b2) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1118 | jNode = (struct jffs2_raw_inode *) get_node_mem(b2->offset, |
| 1119 | pL->readbuf); |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1120 | if (jNode->ino == jDirFoundIno) { |
wdenk | 6c59edc | 2004-05-03 20:45:30 +0000 | [diff] [blame] | 1121 | src = (unsigned char *)jNode + sizeof(struct jffs2_raw_inode); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1122 | |
| 1123 | #if 0 |
| 1124 | putLabeledWord("\t\t dsize = ", jNode->dsize); |
| 1125 | putstr("\t\t target = "); |
| 1126 | putnstr(src, jNode->dsize); |
| 1127 | putstr("\r\n"); |
| 1128 | #endif |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 1129 | strncpy(tmp, (char *)src, jNode->dsize); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1130 | tmp[jNode->dsize] = '\0'; |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1131 | put_fl_mem(jNode, pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1132 | break; |
| 1133 | } |
| 1134 | b2 = b2->next; |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1135 | put_fl_mem(jNode, pL->readbuf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1136 | } |
| 1137 | /* ok so the name of the new file to find is in tmp */ |
| 1138 | /* if it starts with a slash it is root based else shared dirs */ |
| 1139 | if (tmp[0] == '/') |
| 1140 | pino = 1; |
| 1141 | else |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1142 | pino = jDirFoundPino; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1143 | |
| 1144 | return jffs2_1pass_search_inode(pL, tmp, pino); |
| 1145 | } |
| 1146 | |
| 1147 | static u32 |
| 1148 | jffs2_1pass_search_list_inodes(struct b_lists * pL, const char *fname, u32 pino) |
| 1149 | { |
| 1150 | int i; |
| 1151 | char tmp[256]; |
| 1152 | char working_tmp[256]; |
| 1153 | char *c; |
| 1154 | |
| 1155 | /* discard any leading slash */ |
| 1156 | i = 0; |
| 1157 | while (fname[i] == '/') |
| 1158 | i++; |
| 1159 | strcpy(tmp, &fname[i]); |
| 1160 | working_tmp[0] = '\0'; |
| 1161 | while ((c = (char *) strchr(tmp, '/'))) /* we are still dired searching */ |
| 1162 | { |
| 1163 | strncpy(working_tmp, tmp, c - tmp); |
| 1164 | working_tmp[c - tmp] = '\0'; |
| 1165 | for (i = 0; i < strlen(c) - 1; i++) |
| 1166 | tmp[i] = c[i + 1]; |
| 1167 | tmp[i] = '\0'; |
| 1168 | /* only a failure if we arent looking at top level */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1169 | if (!(pino = jffs2_1pass_find_inode(pL, working_tmp, pino)) && |
| 1170 | (working_tmp[0])) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1171 | putstr("find_inode failed for name="); |
| 1172 | putstr(working_tmp); |
| 1173 | putstr("\r\n"); |
| 1174 | return 0; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | if (tmp[0] && !(pino = jffs2_1pass_find_inode(pL, tmp, pino))) { |
| 1179 | putstr("find_inode failed for name="); |
| 1180 | putstr(tmp); |
| 1181 | putstr("\r\n"); |
| 1182 | return 0; |
| 1183 | } |
| 1184 | /* this is for the bare filename, directories have already been mapped */ |
| 1185 | if (!(pino = jffs2_1pass_list_inodes(pL, pino))) { |
| 1186 | putstr("find_inode failed for name="); |
| 1187 | putstr(tmp); |
| 1188 | putstr("\r\n"); |
| 1189 | return 0; |
| 1190 | } |
| 1191 | return pino; |
| 1192 | |
| 1193 | } |
| 1194 | |
| 1195 | unsigned char |
| 1196 | jffs2_1pass_rescan_needed(struct part_info *part) |
| 1197 | { |
| 1198 | struct b_node *b; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1199 | struct jffs2_unknown_node onode; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1200 | struct jffs2_unknown_node *node; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1201 | struct b_lists *pL = (struct b_lists *)part->jffs2_priv; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1202 | |
| 1203 | if (part->jffs2_priv == 0){ |
| 1204 | DEBUGF ("rescan: First time in use\n"); |
| 1205 | return 1; |
| 1206 | } |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 1207 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1208 | /* if we have no list, we need to rescan */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1209 | if (pL->frag.listCount == 0) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1210 | DEBUGF ("rescan: fraglist zero\n"); |
| 1211 | return 1; |
| 1212 | } |
| 1213 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1214 | /* but suppose someone reflashed a partition at the same offset... */ |
| 1215 | b = pL->dir.listHead; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1216 | while (b) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1217 | node = (struct jffs2_unknown_node *) get_fl_mem(b->offset, |
| 1218 | sizeof(onode), &onode); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1219 | if (node->nodetype != JFFS2_NODETYPE_DIRENT) { |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1220 | DEBUGF ("rescan: fs changed beneath me? (%lx)\n", |
| 1221 | (unsigned long) b->offset); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1222 | return 1; |
| 1223 | } |
| 1224 | b = b->next; |
| 1225 | } |
| 1226 | return 0; |
| 1227 | } |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1228 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1229 | #ifdef CONFIG_JFFS2_SUMMARY |
| 1230 | static u32 sum_get_unaligned32(u32 *ptr) |
| 1231 | { |
| 1232 | u32 val; |
| 1233 | u8 *p = (u8 *)ptr; |
| 1234 | |
| 1235 | val = *p | (*(p + 1) << 8) | (*(p + 2) << 16) | (*(p + 3) << 24); |
| 1236 | |
| 1237 | return __le32_to_cpu(val); |
| 1238 | } |
| 1239 | |
| 1240 | static u16 sum_get_unaligned16(u16 *ptr) |
| 1241 | { |
| 1242 | u16 val; |
| 1243 | u8 *p = (u8 *)ptr; |
| 1244 | |
| 1245 | val = *p | (*(p + 1) << 8); |
| 1246 | |
| 1247 | return __le16_to_cpu(val); |
| 1248 | } |
| 1249 | |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1250 | #define dbg_summary(...) do {} while (0); |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1251 | /* |
| 1252 | * Process the stored summary information - helper function for |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1253 | * jffs2_sum_scan_sumnode() |
| 1254 | */ |
| 1255 | |
| 1256 | static int jffs2_sum_process_sum_data(struct part_info *part, uint32_t offset, |
| 1257 | struct jffs2_raw_summary *summary, |
| 1258 | struct b_lists *pL) |
| 1259 | { |
| 1260 | void *sp; |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1261 | int i, pass; |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1262 | struct b_node *b; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1263 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1264 | for (pass = 0; pass < 2; pass++) { |
| 1265 | sp = summary->sum; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1266 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1267 | for (i = 0; i < summary->sum_num; i++) { |
| 1268 | struct jffs2_sum_unknown_flash *spu = sp; |
| 1269 | dbg_summary("processing summary index %d\n", i); |
Ilya 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 | switch (sum_get_unaligned16(&spu->nodetype)) { |
| 1272 | case JFFS2_NODETYPE_INODE: { |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1273 | struct jffs2_sum_inode_flash *spi; |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1274 | if (pass) { |
| 1275 | spi = sp; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1276 | |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1277 | b = insert_node(&pL->frag); |
| 1278 | if (!b) |
| 1279 | return -1; |
| 1280 | b->offset = (u32)part->offset + |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1281 | offset + |
| 1282 | sum_get_unaligned32( |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1283 | &spi->offset); |
| 1284 | b->version = sum_get_unaligned32( |
| 1285 | &spi->version); |
| 1286 | b->ino = sum_get_unaligned32( |
| 1287 | &spi->inode); |
Wagner Popov dos Santos | b9e3b67 | 2021-02-23 00:49:00 -0300 | [diff] [blame] | 1288 | b->datacrc = CRC_UNKNOWN; |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1289 | } |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1290 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1291 | sp += JFFS2_SUMMARY_INODE_SIZE; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1292 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1293 | break; |
| 1294 | } |
| 1295 | case JFFS2_NODETYPE_DIRENT: { |
| 1296 | struct jffs2_sum_dirent_flash *spd; |
| 1297 | spd = sp; |
| 1298 | if (pass) { |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1299 | b = insert_node(&pL->dir); |
| 1300 | if (!b) |
| 1301 | return -1; |
| 1302 | b->offset = (u32)part->offset + |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1303 | offset + |
| 1304 | sum_get_unaligned32( |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1305 | &spd->offset); |
| 1306 | b->version = sum_get_unaligned32( |
| 1307 | &spd->version); |
| 1308 | b->pino = sum_get_unaligned32( |
| 1309 | &spd->pino); |
Wagner Popov dos Santos | b9e3b67 | 2021-02-23 00:49:00 -0300 | [diff] [blame] | 1310 | b->datacrc = CRC_UNKNOWN; |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1311 | } |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1312 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1313 | sp += JFFS2_SUMMARY_DIRENT_SIZE( |
| 1314 | spd->nsize); |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1315 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1316 | break; |
| 1317 | } |
| 1318 | default : { |
| 1319 | uint16_t nodetype = sum_get_unaligned16( |
| 1320 | &spu->nodetype); |
| 1321 | printf("Unsupported node type %x found" |
| 1322 | " in summary!\n", |
| 1323 | nodetype); |
| 1324 | if ((nodetype & JFFS2_COMPAT_MASK) == |
| 1325 | JFFS2_FEATURE_INCOMPAT) |
| 1326 | return -EIO; |
| 1327 | return -EBADMSG; |
| 1328 | } |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | } |
| 1332 | return 0; |
| 1333 | } |
| 1334 | |
| 1335 | /* Process the summary node - called from jffs2_scan_eraseblock() */ |
| 1336 | int jffs2_sum_scan_sumnode(struct part_info *part, uint32_t offset, |
| 1337 | struct jffs2_raw_summary *summary, uint32_t sumsize, |
| 1338 | struct b_lists *pL) |
| 1339 | { |
| 1340 | struct jffs2_unknown_node crcnode; |
Tom Rini | 7c4734d | 2016-03-27 14:48:36 -0400 | [diff] [blame] | 1341 | int ret, __maybe_unused ofs; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1342 | uint32_t crc; |
| 1343 | |
| 1344 | ofs = part->sector_size - sumsize; |
| 1345 | |
| 1346 | dbg_summary("summary found for 0x%08x at 0x%08x (0x%x bytes)\n", |
| 1347 | offset, offset + ofs, sumsize); |
| 1348 | |
| 1349 | /* OK, now check for node validity and CRC */ |
| 1350 | crcnode.magic = JFFS2_MAGIC_BITMASK; |
| 1351 | crcnode.nodetype = JFFS2_NODETYPE_SUMMARY; |
| 1352 | crcnode.totlen = summary->totlen; |
| 1353 | crc = crc32_no_comp(0, (uchar *)&crcnode, sizeof(crcnode)-4); |
| 1354 | |
| 1355 | if (summary->hdr_crc != crc) { |
| 1356 | dbg_summary("Summary node header is corrupt (bad CRC or " |
| 1357 | "no summary at all)\n"); |
| 1358 | goto crc_err; |
| 1359 | } |
| 1360 | |
| 1361 | if (summary->totlen != sumsize) { |
| 1362 | dbg_summary("Summary node is corrupt (wrong erasesize?)\n"); |
| 1363 | goto crc_err; |
| 1364 | } |
| 1365 | |
| 1366 | crc = crc32_no_comp(0, (uchar *)summary, |
| 1367 | sizeof(struct jffs2_raw_summary)-8); |
| 1368 | |
| 1369 | if (summary->node_crc != crc) { |
| 1370 | dbg_summary("Summary node is corrupt (bad CRC)\n"); |
| 1371 | goto crc_err; |
| 1372 | } |
| 1373 | |
| 1374 | crc = crc32_no_comp(0, (uchar *)summary->sum, |
| 1375 | sumsize - sizeof(struct jffs2_raw_summary)); |
| 1376 | |
| 1377 | if (summary->sum_crc != crc) { |
| 1378 | dbg_summary("Summary node data is corrupt (bad CRC)\n"); |
| 1379 | goto crc_err; |
| 1380 | } |
| 1381 | |
| 1382 | if (summary->cln_mkr) |
| 1383 | dbg_summary("Summary : CLEANMARKER node \n"); |
| 1384 | |
| 1385 | ret = jffs2_sum_process_sum_data(part, offset, summary, pL); |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1386 | if (ret == -EBADMSG) |
| 1387 | return 0; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1388 | if (ret) |
| 1389 | return ret; /* real error */ |
| 1390 | |
| 1391 | return 1; |
| 1392 | |
| 1393 | crc_err: |
| 1394 | putstr("Summary node crc error, skipping summary information.\n"); |
| 1395 | |
| 1396 | return 0; |
| 1397 | } |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1398 | #endif /* CONFIG_JFFS2_SUMMARY */ |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1399 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1400 | #ifdef DEBUG_FRAGMENTS |
| 1401 | static void |
| 1402 | dump_fragments(struct b_lists *pL) |
| 1403 | { |
| 1404 | struct b_node *b; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1405 | struct jffs2_raw_inode ojNode; |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1406 | struct jffs2_raw_inode *jNode; |
| 1407 | |
| 1408 | putstr("\r\n\r\n******The fragment Entries******\r\n"); |
| 1409 | b = pL->frag.listHead; |
| 1410 | while (b) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1411 | jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, |
| 1412 | sizeof(ojNode), &ojNode); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1413 | putLabeledWord("\r\n\tbuild_list: FLASH_OFFSET = ", b->offset); |
| 1414 | putLabeledWord("\tbuild_list: totlen = ", jNode->totlen); |
| 1415 | putLabeledWord("\tbuild_list: inode = ", jNode->ino); |
| 1416 | putLabeledWord("\tbuild_list: version = ", jNode->version); |
| 1417 | putLabeledWord("\tbuild_list: isize = ", jNode->isize); |
| 1418 | putLabeledWord("\tbuild_list: atime = ", jNode->atime); |
| 1419 | putLabeledWord("\tbuild_list: offset = ", jNode->offset); |
| 1420 | putLabeledWord("\tbuild_list: csize = ", jNode->csize); |
| 1421 | putLabeledWord("\tbuild_list: dsize = ", jNode->dsize); |
| 1422 | putLabeledWord("\tbuild_list: compr = ", jNode->compr); |
| 1423 | putLabeledWord("\tbuild_list: usercompr = ", jNode->usercompr); |
| 1424 | putLabeledWord("\tbuild_list: flags = ", jNode->flags); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1425 | putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1426 | b = b->next; |
| 1427 | } |
| 1428 | } |
| 1429 | #endif |
| 1430 | |
| 1431 | #ifdef DEBUG_DIRENTS |
| 1432 | static void |
| 1433 | dump_dirents(struct b_lists *pL) |
| 1434 | { |
| 1435 | struct b_node *b; |
| 1436 | struct jffs2_raw_dirent *jDir; |
| 1437 | |
| 1438 | putstr("\r\n\r\n******The directory Entries******\r\n"); |
| 1439 | b = pL->dir.listHead; |
| 1440 | while (b) { |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1441 | jDir = (struct jffs2_raw_dirent *) get_node_mem(b->offset, |
| 1442 | pL->readbuf); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1443 | putstr("\r\n"); |
| 1444 | putnstr(jDir->name, jDir->nsize); |
| 1445 | putLabeledWord("\r\n\tbuild_list: magic = ", jDir->magic); |
| 1446 | putLabeledWord("\tbuild_list: nodetype = ", jDir->nodetype); |
| 1447 | putLabeledWord("\tbuild_list: hdr_crc = ", jDir->hdr_crc); |
| 1448 | putLabeledWord("\tbuild_list: pino = ", jDir->pino); |
| 1449 | putLabeledWord("\tbuild_list: version = ", jDir->version); |
| 1450 | putLabeledWord("\tbuild_list: ino = ", jDir->ino); |
| 1451 | putLabeledWord("\tbuild_list: mctime = ", jDir->mctime); |
| 1452 | putLabeledWord("\tbuild_list: nsize = ", jDir->nsize); |
| 1453 | putLabeledWord("\tbuild_list: type = ", jDir->type); |
| 1454 | putLabeledWord("\tbuild_list: node_crc = ", jDir->node_crc); |
| 1455 | putLabeledWord("\tbuild_list: name_crc = ", jDir->name_crc); |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1456 | putLabeledWord("\tbuild_list: offset = ", b->offset); /* FIXME: ? [RS] */ |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1457 | b = b->next; |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1458 | put_fl_mem(jDir, pL->readbuf); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1462 | |
Mark Tomlinson | f89bfd0 | 2015-07-01 16:38:27 +1200 | [diff] [blame] | 1463 | #define DEFAULT_EMPTY_SCAN_SIZE 256 |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1464 | |
| 1465 | static inline uint32_t EMPTY_SCAN_SIZE(uint32_t sector_size) |
| 1466 | { |
| 1467 | if (sector_size < DEFAULT_EMPTY_SCAN_SIZE) |
| 1468 | return sector_size; |
| 1469 | else |
| 1470 | return DEFAULT_EMPTY_SCAN_SIZE; |
| 1471 | } |
| 1472 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1473 | static u32 |
| 1474 | jffs2_1pass_build_lists(struct part_info * part) |
| 1475 | { |
| 1476 | struct b_lists *pL; |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1477 | union jffs2_node_union *node; |
Tom Rini | 3cd84d6 | 2013-12-05 14:48:38 -0500 | [diff] [blame] | 1478 | u32 nr_sectors; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1479 | u32 i; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1480 | u32 counter4 = 0; |
| 1481 | u32 counterF = 0; |
| 1482 | u32 counterN = 0; |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1483 | u32 max_totlen = 0; |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1484 | u32 buf_size; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1485 | char *buf; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1486 | |
Tom Rini | 3cd84d6 | 2013-12-05 14:48:38 -0500 | [diff] [blame] | 1487 | nr_sectors = lldiv(part->size, part->sector_size); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1488 | /* turn off the lcd. Refreshing the lcd adds 50% overhead to the */ |
| 1489 | /* jffs2 list building enterprise nope. in newer versions the overhead is */ |
| 1490 | /* only about 5 %. not enough to inconvenience people for. */ |
| 1491 | /* lcd_off(); */ |
| 1492 | |
| 1493 | /* if we are building a list we need to refresh the cache. */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1494 | jffs_init_1pass_list(part); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1495 | pL = (struct b_lists *)part->jffs2_priv; |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1496 | buf = malloc(DEFAULT_EMPTY_SCAN_SIZE); |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1497 | puts ("Scanning JFFS2 FS: "); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1498 | |
| 1499 | /* start at the beginning of the partition */ |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1500 | for (i = 0; i < nr_sectors; i++) { |
| 1501 | uint32_t sector_ofs = i * part->sector_size; |
| 1502 | uint32_t buf_ofs = sector_ofs; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1503 | uint32_t buf_len; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1504 | uint32_t ofs, prevofs; |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1505 | #ifdef CONFIG_JFFS2_SUMMARY |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1506 | struct jffs2_sum_marker *sm; |
| 1507 | void *sumptr = NULL; |
| 1508 | uint32_t sumlen; |
| 1509 | int ret; |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1510 | #endif |
Mark Tomlinson | 56a7f48 | 2015-07-01 16:38:28 +1200 | [diff] [blame] | 1511 | /* Indicates a sector with a CLEANMARKER was found */ |
| 1512 | int clean_sector = 0; |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1513 | struct jffs2_unknown_node crcnode; |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1514 | struct b_node *b; |
wdenk | 7ad5e4c | 2004-04-25 15:41:35 +0000 | [diff] [blame] | 1515 | |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1516 | /* Set buf_size to maximum length */ |
| 1517 | buf_size = DEFAULT_EMPTY_SCAN_SIZE; |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 1518 | schedule(); |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1519 | |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1520 | #ifdef CONFIG_JFFS2_SUMMARY |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1521 | buf_len = sizeof(*sm); |
| 1522 | |
| 1523 | /* Read as much as we want into the _end_ of the preallocated |
| 1524 | * buffer |
| 1525 | */ |
| 1526 | get_fl_mem(part->offset + sector_ofs + part->sector_size - |
| 1527 | buf_len, buf_len, buf + buf_size - buf_len); |
| 1528 | |
| 1529 | sm = (void *)buf + buf_size - sizeof(*sm); |
| 1530 | if (sm->magic == JFFS2_SUM_MAGIC) { |
| 1531 | sumlen = part->sector_size - sm->offset; |
| 1532 | sumptr = buf + buf_size - sumlen; |
| 1533 | |
| 1534 | /* Now, make sure the summary itself is available */ |
| 1535 | if (sumlen > buf_size) { |
| 1536 | /* Need to kmalloc for this. */ |
| 1537 | sumptr = malloc(sumlen); |
| 1538 | if (!sumptr) { |
| 1539 | putstr("Can't get memory for summary " |
| 1540 | "node!\n"); |
Ilya Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1541 | free(buf); |
| 1542 | jffs2_free_cache(part); |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1543 | return 0; |
| 1544 | } |
| 1545 | memcpy(sumptr + sumlen - buf_len, buf + |
| 1546 | buf_size - buf_len, buf_len); |
| 1547 | } |
| 1548 | if (buf_len < sumlen) { |
| 1549 | /* Need to read more so that the entire summary |
| 1550 | * node is present |
| 1551 | */ |
| 1552 | get_fl_mem(part->offset + sector_ofs + |
| 1553 | part->sector_size - sumlen, |
| 1554 | sumlen - buf_len, sumptr); |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | if (sumptr) { |
| 1559 | ret = jffs2_sum_scan_sumnode(part, sector_ofs, sumptr, |
| 1560 | sumlen, pL); |
| 1561 | |
| 1562 | if (buf_size && sumlen > buf_size) |
| 1563 | free(sumptr); |
Ilya Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1564 | if (ret < 0) { |
| 1565 | free(buf); |
| 1566 | jffs2_free_cache(part); |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1567 | return 0; |
Ilya Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1568 | } |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1569 | if (ret) |
| 1570 | continue; |
| 1571 | |
| 1572 | } |
Ilya Yanok | bcaa1f7 | 2009-07-17 15:02:42 +0400 | [diff] [blame] | 1573 | #endif /* CONFIG_JFFS2_SUMMARY */ |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1574 | |
| 1575 | buf_len = EMPTY_SCAN_SIZE(part->sector_size); |
| 1576 | |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1577 | get_fl_mem((u32)part->offset + buf_ofs, buf_len, buf); |
Stuart Wood | 471a2f0 | 2008-06-02 16:40:08 -0400 | [diff] [blame] | 1578 | |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1579 | /* We temporarily use 'ofs' as a pointer into the buffer/jeb */ |
| 1580 | ofs = 0; |
| 1581 | |
| 1582 | /* Scan only 4KiB of 0xFF before declaring it's empty */ |
| 1583 | while (ofs < EMPTY_SCAN_SIZE(part->sector_size) && |
| 1584 | *(uint32_t *)(&buf[ofs]) == 0xFFFFFFFF) |
| 1585 | ofs += 4; |
| 1586 | |
| 1587 | if (ofs == EMPTY_SCAN_SIZE(part->sector_size)) |
| 1588 | continue; |
| 1589 | |
| 1590 | ofs += sector_ofs; |
| 1591 | prevofs = ofs - 1; |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1592 | /* |
| 1593 | * Set buf_size down to the minimum size required. |
| 1594 | * This prevents reading in chunks of flash data unnecessarily. |
| 1595 | */ |
| 1596 | buf_size = sizeof(union jffs2_node_union); |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1597 | |
| 1598 | scan_more: |
| 1599 | while (ofs < sector_ofs + part->sector_size) { |
| 1600 | if (ofs == prevofs) { |
| 1601 | printf("offset %08x already seen, skip\n", ofs); |
| 1602 | ofs += 4; |
| 1603 | counter4++; |
| 1604 | continue; |
| 1605 | } |
| 1606 | prevofs = ofs; |
| 1607 | if (sector_ofs + part->sector_size < |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1608 | ofs + sizeof(struct jffs2_unknown_node)) |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1609 | break; |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1610 | if (buf_ofs + buf_len < |
| 1611 | ofs + sizeof(struct jffs2_unknown_node)) { |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1612 | buf_len = min_t(uint32_t, buf_size, sector_ofs |
| 1613 | + part->sector_size - ofs); |
| 1614 | get_fl_mem((u32)part->offset + ofs, buf_len, |
| 1615 | buf); |
| 1616 | buf_ofs = ofs; |
| 1617 | } |
| 1618 | |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1619 | node = (union jffs2_node_union *)&buf[ofs - buf_ofs]; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1620 | |
| 1621 | if (*(uint32_t *)(&buf[ofs-buf_ofs]) == 0xffffffff) { |
| 1622 | uint32_t inbuf_ofs; |
Wolfgang Denk | bf913c1 | 2011-10-05 22:58:11 +0200 | [diff] [blame] | 1623 | uint32_t scan_end; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1624 | |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1625 | ofs += 4; |
| 1626 | scan_end = min_t(uint32_t, EMPTY_SCAN_SIZE( |
| 1627 | part->sector_size)/8, |
| 1628 | buf_len); |
| 1629 | more_empty: |
| 1630 | inbuf_ofs = ofs - buf_ofs; |
| 1631 | while (inbuf_ofs < scan_end) { |
| 1632 | if (*(uint32_t *)(&buf[inbuf_ofs]) != |
| 1633 | 0xffffffff) |
| 1634 | goto scan_more; |
| 1635 | |
| 1636 | inbuf_ofs += 4; |
| 1637 | ofs += 4; |
| 1638 | } |
| 1639 | /* Ran off end. */ |
Mark Tomlinson | 56a7f48 | 2015-07-01 16:38:28 +1200 | [diff] [blame] | 1640 | /* |
| 1641 | * If this sector had a clean marker at the |
| 1642 | * beginning, and immediately following this |
| 1643 | * have been a bunch of FF bytes, treat the |
| 1644 | * entire sector as empty. |
| 1645 | */ |
| 1646 | if (clean_sector) |
| 1647 | break; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1648 | |
| 1649 | /* See how much more there is to read in this |
| 1650 | * eraseblock... |
| 1651 | */ |
| 1652 | buf_len = min_t(uint32_t, buf_size, |
| 1653 | sector_ofs + |
| 1654 | part->sector_size - ofs); |
| 1655 | if (!buf_len) { |
| 1656 | /* No more to read. Break out of main |
| 1657 | * loop without marking this range of |
| 1658 | * empty space as dirty (because it's |
| 1659 | * not) |
| 1660 | */ |
| 1661 | break; |
| 1662 | } |
| 1663 | scan_end = buf_len; |
| 1664 | get_fl_mem((u32)part->offset + ofs, buf_len, |
| 1665 | buf); |
| 1666 | buf_ofs = ofs; |
| 1667 | goto more_empty; |
| 1668 | } |
Mark Tomlinson | 56a7f48 | 2015-07-01 16:38:28 +1200 | [diff] [blame] | 1669 | /* |
| 1670 | * Found something not erased in the sector, so reset |
| 1671 | * the 'clean_sector' flag. |
| 1672 | */ |
| 1673 | clean_sector = 0; |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1674 | if (node->u.magic != JFFS2_MAGIC_BITMASK) { |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1675 | ofs += 4; |
| 1676 | counter4++; |
| 1677 | continue; |
| 1678 | } |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1679 | |
| 1680 | crcnode.magic = node->u.magic; |
| 1681 | crcnode.nodetype = node->u.nodetype | JFFS2_NODE_ACCURATE; |
| 1682 | crcnode.totlen = node->u.totlen; |
| 1683 | crcnode.hdr_crc = node->u.hdr_crc; |
| 1684 | if (!hdr_crc(&crcnode)) { |
| 1685 | ofs += 4; |
| 1686 | counter4++; |
| 1687 | continue; |
| 1688 | } |
| 1689 | |
| 1690 | if (ofs + node->u.totlen > sector_ofs + part->sector_size) { |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1691 | ofs += 4; |
| 1692 | counter4++; |
| 1693 | continue; |
| 1694 | } |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1695 | |
| 1696 | if (!(node->u.nodetype & JFFS2_NODE_ACCURATE)) { |
| 1697 | DEBUGF("Obsolete node type: %x len %d offset 0x%x\n", |
| 1698 | node->u.nodetype, node->u.totlen, ofs); |
| 1699 | ofs += ((node->u.totlen + 3) & ~3); |
| 1700 | counterF++; |
| 1701 | continue; |
| 1702 | } |
| 1703 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1704 | /* if its a fragment add it */ |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1705 | switch (node->u.nodetype) { |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1706 | case JFFS2_NODETYPE_INODE: |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1707 | if (buf_ofs + buf_len < |
| 1708 | ofs + sizeof(struct jffs2_raw_inode)) { |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1709 | buf_len = min_t(uint32_t, |
| 1710 | sizeof(struct jffs2_raw_inode), |
| 1711 | sector_ofs + |
| 1712 | part->sector_size - |
| 1713 | ofs); |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1714 | get_fl_mem((u32)part->offset + ofs, |
| 1715 | buf_len, buf); |
| 1716 | buf_ofs = ofs; |
| 1717 | node = (void *)buf; |
| 1718 | } |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1719 | if (!inode_crc((struct jffs2_raw_inode *)node)) |
| 1720 | break; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1721 | |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1722 | b = insert_node(&pL->frag); |
| 1723 | if (!b) { |
Ilya Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1724 | free(buf); |
| 1725 | jffs2_free_cache(part); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1726 | return 0; |
Ilya Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1727 | } |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1728 | b->offset = (u32)part->offset + ofs; |
| 1729 | b->version = node->i.version; |
| 1730 | b->ino = node->i.ino; |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1731 | if (max_totlen < node->u.totlen) |
| 1732 | max_totlen = node->u.totlen; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1733 | break; |
| 1734 | case JFFS2_NODETYPE_DIRENT: |
| 1735 | if (buf_ofs + buf_len < ofs + sizeof(struct |
| 1736 | jffs2_raw_dirent) + |
| 1737 | ((struct |
| 1738 | jffs2_raw_dirent *) |
| 1739 | node)->nsize) { |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1740 | buf_len = min_t(uint32_t, |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1741 | node->u.totlen, |
Mark Tomlinson | ef61982 | 2015-07-01 16:38:26 +1200 | [diff] [blame] | 1742 | sector_ofs + |
| 1743 | part->sector_size - |
| 1744 | ofs); |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1745 | get_fl_mem((u32)part->offset + ofs, |
| 1746 | buf_len, buf); |
| 1747 | buf_ofs = ofs; |
| 1748 | node = (void *)buf; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1749 | } |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1750 | |
| 1751 | if (!dirent_crc((struct jffs2_raw_dirent *) |
| 1752 | node) || |
| 1753 | !dirent_name_crc( |
| 1754 | (struct |
| 1755 | jffs2_raw_dirent *) |
| 1756 | node)) |
| 1757 | break; |
wdenk | 7ad5e4c | 2004-04-25 15:41:35 +0000 | [diff] [blame] | 1758 | if (! (counterN%100)) |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1759 | puts ("\b\b. "); |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1760 | b = insert_node(&pL->dir); |
| 1761 | if (!b) { |
Ilya Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1762 | free(buf); |
| 1763 | jffs2_free_cache(part); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1764 | return 0; |
Ilya Yanok | a87011e | 2009-08-12 16:42:48 +0400 | [diff] [blame] | 1765 | } |
Petr Borsodi | 80c25b1 | 2020-05-07 12:25:56 +0200 | [diff] [blame] | 1766 | b->offset = (u32)part->offset + ofs; |
| 1767 | b->version = node->d.version; |
| 1768 | b->pino = node->d.pino; |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1769 | if (max_totlen < node->u.totlen) |
| 1770 | max_totlen = node->u.totlen; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1771 | counterN++; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1772 | break; |
| 1773 | case JFFS2_NODETYPE_CLEANMARKER: |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1774 | if (node->u.totlen != sizeof(struct jffs2_unknown_node)) |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1775 | printf("OOPS Cleanmarker has bad size " |
Wolfgang Denk | 509cd07 | 2008-07-14 15:06:35 +0200 | [diff] [blame] | 1776 | "%d != %zu\n", |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1777 | node->u.totlen, |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1778 | sizeof(struct jffs2_unknown_node)); |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1779 | if (node->u.totlen == |
| 1780 | sizeof(struct jffs2_unknown_node) && |
| 1781 | ofs == sector_ofs) { |
Mark Tomlinson | 56a7f48 | 2015-07-01 16:38:28 +1200 | [diff] [blame] | 1782 | /* |
| 1783 | * Found a CLEANMARKER at the beginning |
| 1784 | * of the sector. It's in the correct |
| 1785 | * place with correct size and CRC. |
| 1786 | */ |
| 1787 | clean_sector = 1; |
| 1788 | } |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1789 | break; |
| 1790 | case JFFS2_NODETYPE_PADDING: |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1791 | if (node->u.totlen < |
| 1792 | sizeof(struct jffs2_unknown_node)) |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1793 | printf("OOPS Padding has bad size " |
Wolfgang Denk | 509cd07 | 2008-07-14 15:06:35 +0200 | [diff] [blame] | 1794 | "%d < %zu\n", |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1795 | node->u.totlen, |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1796 | sizeof(struct jffs2_unknown_node)); |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1797 | break; |
Ilya Yanok | a933bd6 | 2008-11-13 19:49:35 +0300 | [diff] [blame] | 1798 | case JFFS2_NODETYPE_SUMMARY: |
| 1799 | break; |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1800 | default: |
Wolfgang Denk | 509cd07 | 2008-07-14 15:06:35 +0200 | [diff] [blame] | 1801 | printf("Unknown node type: %x len %d offset 0x%x\n", |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1802 | node->u.nodetype, |
| 1803 | node->u.totlen, ofs); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1804 | } |
Petr Borsodi | a61d1d1 | 2020-05-07 12:25:55 +0200 | [diff] [blame] | 1805 | ofs += ((node->u.totlen + 3) & ~3); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1806 | counterF++; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1807 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Ilya Yanok | ddb0269 | 2008-11-13 19:49:33 +0300 | [diff] [blame] | 1810 | free(buf); |
Mark Tomlinson | efdc2a6 | 2015-07-01 16:38:29 +1200 | [diff] [blame] | 1811 | #if defined(CONFIG_SYS_JFFS2_SORT_FRAGMENTS) |
| 1812 | /* |
| 1813 | * Sort the lists. |
| 1814 | */ |
| 1815 | sort_list(&pL->frag); |
| 1816 | sort_list(&pL->dir); |
| 1817 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1818 | putstr("\b\b done.\r\n"); /* close off the dots */ |
Ilya Yanok | e7fd671 | 2008-11-13 19:49:34 +0300 | [diff] [blame] | 1819 | |
| 1820 | /* We don't care if malloc failed - then each read operation will |
| 1821 | * allocate its own buffer as necessary (NAND) or will read directly |
| 1822 | * from flash (NOR). |
| 1823 | */ |
| 1824 | pL->readbuf = malloc(max_totlen); |
| 1825 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1826 | /* turn the lcd back on. */ |
| 1827 | /* splash(); */ |
| 1828 | |
| 1829 | #if 0 |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1830 | putLabeledWord("dir entries = ", pL->dir.listCount); |
| 1831 | putLabeledWord("frag entries = ", pL->frag.listCount); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1832 | putLabeledWord("+4 increments = ", counter4); |
| 1833 | putLabeledWord("+file_offset increments = ", counterF); |
| 1834 | |
| 1835 | #endif |
| 1836 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1837 | #ifdef DEBUG_DIRENTS |
| 1838 | dump_dirents(pL); |
| 1839 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1840 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1841 | #ifdef DEBUG_FRAGMENTS |
| 1842 | dump_fragments(pL); |
| 1843 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1844 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1845 | /* give visual feedback that we are done scanning the flash */ |
| 1846 | led_blink(0x0, 0x0, 0x1, 0x1); /* off, forever, on 100ms, off 100ms */ |
| 1847 | return 1; |
| 1848 | } |
| 1849 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1850 | static u32 |
| 1851 | jffs2_1pass_fill_info(struct b_lists * pL, struct b_jffs2_info * piL) |
| 1852 | { |
| 1853 | struct b_node *b; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1854 | struct jffs2_raw_inode ojNode; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1855 | struct jffs2_raw_inode *jNode; |
| 1856 | int i; |
| 1857 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1858 | for (i = 0; i < JFFS2_NUM_COMPR; i++) { |
| 1859 | piL->compr_info[i].num_frags = 0; |
| 1860 | piL->compr_info[i].compr_sum = 0; |
| 1861 | piL->compr_info[i].decompr_sum = 0; |
| 1862 | } |
| 1863 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1864 | b = pL->frag.listHead; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1865 | while (b) { |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 1866 | jNode = (struct jffs2_raw_inode *) get_fl_mem(b->offset, |
| 1867 | sizeof(ojNode), &ojNode); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1868 | if (jNode->compr < JFFS2_NUM_COMPR) { |
| 1869 | piL->compr_info[jNode->compr].num_frags++; |
| 1870 | piL->compr_info[jNode->compr].compr_sum += jNode->csize; |
| 1871 | piL->compr_info[jNode->compr].decompr_sum += jNode->dsize; |
| 1872 | } |
| 1873 | b = b->next; |
| 1874 | } |
| 1875 | return 0; |
| 1876 | } |
| 1877 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1878 | static struct b_lists * |
| 1879 | jffs2_get_list(struct part_info * part, const char *who) |
| 1880 | { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 1881 | /* copy requested part_info struct pointer to global location */ |
| 1882 | current_part = part; |
| 1883 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1884 | if (jffs2_1pass_rescan_needed(part)) { |
| 1885 | if (!jffs2_1pass_build_lists(part)) { |
| 1886 | printf("%s: Failed to scan JFFSv2 file structure\n", who); |
| 1887 | return NULL; |
| 1888 | } |
| 1889 | } |
| 1890 | return (struct b_lists *)part->jffs2_priv; |
| 1891 | } |
| 1892 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1893 | /* Print directory / file contents */ |
| 1894 | u32 |
| 1895 | jffs2_1pass_ls(struct part_info * part, const char *fname) |
| 1896 | { |
| 1897 | struct b_lists *pl; |
Wolfgang Denk | 79117cd | 2005-08-16 09:32:45 +0200 | [diff] [blame] | 1898 | long ret = 1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1899 | u32 inode; |
| 1900 | |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1901 | if (! (pl = jffs2_get_list(part, "ls"))) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1902 | return 0; |
| 1903 | |
| 1904 | if (! (inode = jffs2_1pass_search_list_inodes(pl, fname, 1))) { |
| 1905 | putstr("ls: Failed to scan jffs2 file structure\r\n"); |
| 1906 | return 0; |
| 1907 | } |
wdenk | 7ad5e4c | 2004-04-25 15:41:35 +0000 | [diff] [blame] | 1908 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1909 | #if 0 |
| 1910 | putLabeledWord("found file at inode = ", inode); |
| 1911 | putLabeledWord("read_inode returns = ", ret); |
| 1912 | #endif |
wdenk | 7ad5e4c | 2004-04-25 15:41:35 +0000 | [diff] [blame] | 1913 | |
| 1914 | return ret; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1917 | /* Load a file from flash into memory. fname can be a full path */ |
| 1918 | u32 |
| 1919 | jffs2_1pass_load(char *dest, struct part_info * part, const char *fname) |
| 1920 | { |
| 1921 | |
| 1922 | struct b_lists *pl; |
Wolfgang Denk | 79117cd | 2005-08-16 09:32:45 +0200 | [diff] [blame] | 1923 | long ret = 1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1924 | u32 inode; |
| 1925 | |
| 1926 | if (! (pl = jffs2_get_list(part, "load"))) |
| 1927 | return 0; |
| 1928 | |
| 1929 | if (! (inode = jffs2_1pass_search_inode(pl, fname, 1))) { |
| 1930 | putstr("load: Failed to find inode\r\n"); |
| 1931 | return 0; |
| 1932 | } |
| 1933 | |
| 1934 | /* Resolve symlinks */ |
| 1935 | if (! (inode = jffs2_1pass_resolve_inode(pl, inode))) { |
| 1936 | putstr("load: Failed to resolve inode structure\r\n"); |
| 1937 | return 0; |
| 1938 | } |
| 1939 | |
| 1940 | if ((ret = jffs2_1pass_read_inode(pl, inode, dest)) < 0) { |
| 1941 | putstr("load: Failed to read inode\r\n"); |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | DEBUGF ("load: loaded '%s' to 0x%lx (%ld bytes)\n", fname, |
| 1946 | (unsigned long) dest, ret); |
| 1947 | return ret; |
| 1948 | } |
| 1949 | |
| 1950 | /* Return information about the fs on this partition */ |
| 1951 | u32 |
| 1952 | jffs2_1pass_info(struct part_info * part) |
| 1953 | { |
| 1954 | struct b_jffs2_info info; |
| 1955 | struct b_lists *pl; |
| 1956 | int i; |
| 1957 | |
| 1958 | if (! (pl = jffs2_get_list(part, "info"))) |
| 1959 | return 0; |
| 1960 | |
| 1961 | jffs2_1pass_fill_info(pl, &info); |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 1962 | for (i = 0; i < JFFS2_NUM_COMPR; i++) { |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 1963 | printf ("Compression: %s\n" |
| 1964 | "\tfrag count: %d\n" |
| 1965 | "\tcompressed sum: %d\n" |
| 1966 | "\tuncompressed sum: %d\n", |
| 1967 | compr_names[i], |
| 1968 | info.compr_info[i].num_frags, |
| 1969 | info.compr_info[i].compr_sum, |
| 1970 | info.compr_info[i].decompr_sum); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1971 | } |
| 1972 | return 1; |
| 1973 | } |