Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 2 | /* |
| 3 | * This file is part of UBIFS. |
| 4 | * |
| 5 | * Copyright (C) 2006-2008 Nokia Corporation. |
| 6 | * |
Stefan Roese | 28ea29f | 2010-11-01 17:28:00 +0100 | [diff] [blame] | 7 | * (C) Copyright 2008-2010 |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 8 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 9 | * |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 10 | * Authors: Artem Bityutskiy (Битюцкий Артём) |
| 11 | * Adrian Hunter |
| 12 | */ |
| 13 | |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 14 | #include <env.h> |
Simon Glass | 1a974af | 2019-08-01 09:46:36 -0600 | [diff] [blame] | 15 | #include <gzip.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <malloc.h> |
Simon Glass | a87fc0a | 2015-09-02 17:24:57 -0600 | [diff] [blame] | 18 | #include <memalign.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 19 | #include <asm/global_data.h> |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 20 | #include "ubifs.h" |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 21 | #include <part.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 22 | #include <dm/devres.h> |
Ricardo Ribalda Delgado | a7a743e | 2009-04-27 18:33:33 +0200 | [diff] [blame] | 23 | #include <u-boot/zlib.h> |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 24 | |
AKASHI Takahiro | 1d8d34d | 2019-11-13 09:44:47 +0900 | [diff] [blame] | 25 | #include <linux/compat.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 26 | #include <linux/err.h> |
| 27 | #include <linux/lzo.h> |
| 28 | |
Piotr Wojtaszczyk | e60ab9a | 2024-05-28 17:05:28 +0200 | [diff] [blame] | 29 | #if IS_ENABLED(CONFIG_ZSTD) |
| 30 | #include <linux/zstd.h> |
| 31 | #include <abuf.h> |
| 32 | #endif |
| 33 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
| 36 | /* compress.c */ |
| 37 | |
| 38 | /* |
Ricardo Ribalda Delgado | a7a743e | 2009-04-27 18:33:33 +0200 | [diff] [blame] | 39 | * We need a wrapper for zunzip() because the parameters are |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 40 | * incompatible with the lzo decompressor. |
| 41 | */ |
| 42 | static int gzip_decompress(const unsigned char *in, size_t in_len, |
| 43 | unsigned char *out, size_t *out_len) |
| 44 | { |
Veli-Pekka Peltola | 7b7e155 | 2012-09-05 18:05:14 +0300 | [diff] [blame] | 45 | return zunzip(out, *out_len, (unsigned char *)in, |
| 46 | (unsigned long *)out_len, 0, 0); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Piotr Wojtaszczyk | e60ab9a | 2024-05-28 17:05:28 +0200 | [diff] [blame] | 49 | #if IS_ENABLED(CONFIG_ZSTD) |
| 50 | static int zstd_decompress_wrapper(const unsigned char *in, size_t in_len, |
| 51 | unsigned char *out, size_t *out_len) |
| 52 | { |
| 53 | struct abuf abuf_in, abuf_out; |
| 54 | int ret; |
| 55 | |
| 56 | abuf_init_set(&abuf_in, (void *)in, in_len); |
| 57 | abuf_init_set(&abuf_out, (void *)out, *out_len); |
| 58 | |
| 59 | ret = zstd_decompress(&abuf_in, &abuf_out); |
| 60 | if (ret < 0) |
| 61 | return ret; |
| 62 | |
| 63 | *out_len = ret; |
| 64 | return 0; |
| 65 | } |
| 66 | #endif |
| 67 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 68 | /* Fake description object for the "none" compressor */ |
| 69 | static struct ubifs_compressor none_compr = { |
| 70 | .compr_type = UBIFS_COMPR_NONE, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 71 | .name = "none", |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 72 | .capi_name = "", |
| 73 | .decompress = NULL, |
| 74 | }; |
| 75 | |
| 76 | static struct ubifs_compressor lzo_compr = { |
| 77 | .compr_type = UBIFS_COMPR_LZO, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 78 | #ifndef __UBOOT__ |
| 79 | .comp_mutex = &lzo_mutex, |
| 80 | #endif |
| 81 | .name = "lzo", |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 82 | .capi_name = "lzo", |
| 83 | .decompress = lzo1x_decompress_safe, |
| 84 | }; |
| 85 | |
| 86 | static struct ubifs_compressor zlib_compr = { |
| 87 | .compr_type = UBIFS_COMPR_ZLIB, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 88 | #ifndef __UBOOT__ |
| 89 | .comp_mutex = &deflate_mutex, |
| 90 | .decomp_mutex = &inflate_mutex, |
| 91 | #endif |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 92 | .name = "zlib", |
| 93 | .capi_name = "deflate", |
| 94 | .decompress = gzip_decompress, |
| 95 | }; |
Piotr Wojtaszczyk | e60ab9a | 2024-05-28 17:05:28 +0200 | [diff] [blame] | 96 | |
| 97 | #if IS_ENABLED(CONFIG_ZSTD) |
| 98 | static struct ubifs_compressor zstd_compr = { |
| 99 | .compr_type = UBIFS_COMPR_ZSTD, |
| 100 | #ifndef __UBOOT__ |
| 101 | .comp_mutex = &zstd_enc_mutex, |
| 102 | .decomp_mutex = &zstd_dec_mutex, |
| 103 | #endif |
| 104 | .name = "zstd", |
| 105 | .capi_name = "zstd", |
| 106 | .decompress = zstd_decompress_wrapper, |
| 107 | }; |
| 108 | #endif |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 109 | |
| 110 | /* All UBIFS compressors */ |
Piotr Wojtaszczyk | e60ab9a | 2024-05-28 17:05:28 +0200 | [diff] [blame] | 111 | struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT] = {NULL}; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 112 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 113 | #ifdef __UBOOT__ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 114 | |
| 115 | struct crypto_comp { |
| 116 | int compressor; |
| 117 | }; |
| 118 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 119 | static inline struct crypto_comp |
| 120 | *crypto_alloc_comp(const char *alg_name, u32 type, u32 mask) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 121 | { |
| 122 | struct ubifs_compressor *comp; |
| 123 | struct crypto_comp *ptr; |
| 124 | int i = 0; |
| 125 | |
Marcel Ziswiler | abc574b | 2015-08-18 13:06:37 +0200 | [diff] [blame] | 126 | ptr = malloc_cache_aligned(sizeof(struct crypto_comp)); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 127 | while (i < UBIFS_COMPR_TYPES_CNT) { |
| 128 | comp = ubifs_compressors[i]; |
| 129 | if (!comp) { |
| 130 | i++; |
| 131 | continue; |
| 132 | } |
| 133 | if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) { |
| 134 | ptr->compressor = i; |
| 135 | return ptr; |
| 136 | } |
| 137 | i++; |
| 138 | } |
| 139 | if (i >= UBIFS_COMPR_TYPES_CNT) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 140 | dbg_gen("invalid compression type %s", alg_name); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 141 | free (ptr); |
| 142 | return NULL; |
| 143 | } |
| 144 | return ptr; |
| 145 | } |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 146 | static inline int |
| 147 | crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm, |
| 148 | const u8 *src, unsigned int slen, u8 *dst, |
| 149 | unsigned int *dlen) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 150 | { |
| 151 | struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor]; |
| 152 | int err; |
Paul Davey | 7f91838 | 2018-11-05 18:09:29 +1300 | [diff] [blame] | 153 | size_t tmp_len = *dlen; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 154 | |
| 155 | if (compr->compr_type == UBIFS_COMPR_NONE) { |
| 156 | memcpy(dst, src, slen); |
| 157 | *dlen = slen; |
| 158 | return 0; |
| 159 | } |
| 160 | |
Paul Davey | 7f91838 | 2018-11-05 18:09:29 +1300 | [diff] [blame] | 161 | err = compr->decompress(src, slen, dst, &tmp_len); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 162 | if (err) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 163 | ubifs_err(c, "cannot decompress %d bytes, compressor %s, " |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 164 | "error %d", slen, compr->name, err); |
| 165 | |
Paul Davey | 7f91838 | 2018-11-05 18:09:29 +1300 | [diff] [blame] | 166 | *dlen = tmp_len; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 167 | return err; |
| 168 | |
| 169 | return 0; |
| 170 | } |
Anton Habegger | 62e79dd | 2015-01-22 22:29:10 +0100 | [diff] [blame] | 171 | |
| 172 | /* from shrinker.c */ |
| 173 | |
| 174 | /* Global clean znode counter (for all mounted UBIFS instances) */ |
| 175 | atomic_long_t ubifs_clean_zn_cnt; |
| 176 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 177 | #endif |
| 178 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 179 | /** |
| 180 | * ubifs_decompress - decompress data. |
| 181 | * @in_buf: data to decompress |
| 182 | * @in_len: length of the data to decompress |
| 183 | * @out_buf: output buffer where decompressed data should |
| 184 | * @out_len: output length is returned here |
| 185 | * @compr_type: type of compression |
| 186 | * |
| 187 | * This function decompresses data from buffer @in_buf into buffer @out_buf. |
| 188 | * The length of the uncompressed data is returned in @out_len. This functions |
| 189 | * returns %0 on success or a negative error code on failure. |
| 190 | */ |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 191 | int ubifs_decompress(const struct ubifs_info *c, const void *in_buf, |
| 192 | int in_len, void *out_buf, int *out_len, int compr_type) |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 193 | { |
| 194 | int err; |
| 195 | struct ubifs_compressor *compr; |
| 196 | |
| 197 | if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 198 | ubifs_err(c, "invalid compression type %d", compr_type); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 199 | return -EINVAL; |
| 200 | } |
| 201 | |
| 202 | compr = ubifs_compressors[compr_type]; |
| 203 | |
Piotr Wojtaszczyk | e60ab9a | 2024-05-28 17:05:28 +0200 | [diff] [blame] | 204 | if (unlikely(!compr)) { |
| 205 | ubifs_err(c, "compression type %d is not compiled in", compr_type); |
| 206 | return -EINVAL; |
| 207 | } |
| 208 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 209 | if (unlikely(!compr->capi_name)) { |
Piotr Wojtaszczyk | e60ab9a | 2024-05-28 17:05:28 +0200 | [diff] [blame] | 210 | ubifs_err(c, "%s compression is not compiled in", |
| 211 | compr->name ? compr->name : "unknown"); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 212 | return -EINVAL; |
| 213 | } |
| 214 | |
| 215 | if (compr_type == UBIFS_COMPR_NONE) { |
| 216 | memcpy(out_buf, in_buf, in_len); |
| 217 | *out_len = in_len; |
| 218 | return 0; |
| 219 | } |
| 220 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 221 | if (compr->decomp_mutex) |
| 222 | mutex_lock(compr->decomp_mutex); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 223 | err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 224 | (unsigned int *)out_len); |
| 225 | if (compr->decomp_mutex) |
| 226 | mutex_unlock(compr->decomp_mutex); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 227 | if (err) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 228 | ubifs_err(c, "cannot decompress %d bytes, compressor %s," |
| 229 | " error %d", in_len, compr->name, err); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 230 | |
| 231 | return err; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * compr_init - initialize a compressor. |
| 236 | * @compr: compressor description object |
| 237 | * |
| 238 | * This function initializes the requested compressor and returns zero in case |
| 239 | * of success or a negative error code in case of failure. |
| 240 | */ |
| 241 | static int __init compr_init(struct ubifs_compressor *compr) |
| 242 | { |
| 243 | ubifs_compressors[compr->compr_type] = compr; |
Peter Tyser | 9057cbf | 2009-09-21 11:20:36 -0500 | [diff] [blame] | 244 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 245 | if (compr->capi_name) { |
| 246 | compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0); |
| 247 | if (IS_ERR(compr->cc)) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 248 | dbg_gen("cannot initialize compressor %s," |
| 249 | " error %ld", compr->name, |
| 250 | PTR_ERR(compr->cc)); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 251 | return PTR_ERR(compr->cc); |
| 252 | } |
| 253 | } |
| 254 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * ubifs_compressors_init - initialize UBIFS compressors. |
| 260 | * |
| 261 | * This function initializes the compressor which were compiled in. Returns |
| 262 | * zero in case of success and a negative error code in case of failure. |
| 263 | */ |
| 264 | int __init ubifs_compressors_init(void) |
| 265 | { |
| 266 | int err; |
| 267 | |
| 268 | err = compr_init(&lzo_compr); |
| 269 | if (err) |
| 270 | return err; |
| 271 | |
| 272 | err = compr_init(&zlib_compr); |
| 273 | if (err) |
| 274 | return err; |
| 275 | |
Piotr Wojtaszczyk | e60ab9a | 2024-05-28 17:05:28 +0200 | [diff] [blame] | 276 | #if IS_ENABLED(CONFIG_ZSTD) |
| 277 | err = compr_init(&zstd_compr); |
| 278 | if (err) |
| 279 | return err; |
| 280 | #endif |
| 281 | |
Michael Lawnick | b061701 | 2009-03-19 10:06:41 +0100 | [diff] [blame] | 282 | err = compr_init(&none_compr); |
| 283 | if (err) |
| 284 | return err; |
| 285 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * ubifsls... |
| 291 | */ |
| 292 | |
| 293 | static int filldir(struct ubifs_info *c, const char *name, int namlen, |
| 294 | u64 ino, unsigned int d_type) |
| 295 | { |
| 296 | struct inode *inode; |
| 297 | char filetime[32]; |
| 298 | |
| 299 | switch (d_type) { |
| 300 | case UBIFS_ITYPE_REG: |
| 301 | printf("\t"); |
| 302 | break; |
| 303 | case UBIFS_ITYPE_DIR: |
| 304 | printf("<DIR>\t"); |
| 305 | break; |
| 306 | case UBIFS_ITYPE_LNK: |
| 307 | printf("<LNK>\t"); |
| 308 | break; |
| 309 | default: |
| 310 | printf("other\t"); |
| 311 | break; |
| 312 | } |
| 313 | |
| 314 | inode = ubifs_iget(c->vfs_sb, ino); |
| 315 | if (IS_ERR(inode)) { |
| 316 | printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n", |
| 317 | __func__, ino, inode); |
| 318 | return -1; |
| 319 | } |
| 320 | ctime_r((time_t *)&inode->i_mtime, filetime); |
| 321 | printf("%9lld %24.24s ", inode->i_size, filetime); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 322 | #ifndef __UBOOT__ |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 323 | ubifs_iput(inode); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 324 | #endif |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 325 | |
| 326 | printf("%s\n", name); |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static int ubifs_printdir(struct file *file, void *dirent) |
| 332 | { |
| 333 | int err, over = 0; |
| 334 | struct qstr nm; |
| 335 | union ubifs_key key; |
| 336 | struct ubifs_dent_node *dent; |
| 337 | struct inode *dir = file->f_path.dentry->d_inode; |
| 338 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
| 339 | |
| 340 | dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos); |
| 341 | |
| 342 | if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2) |
| 343 | /* |
| 344 | * The directory was seek'ed to a senseless position or there |
| 345 | * are no more entries. |
| 346 | */ |
| 347 | return 0; |
| 348 | |
| 349 | if (file->f_pos == 1) { |
| 350 | /* Find the first entry in TNC and save it */ |
| 351 | lowest_dent_key(c, &key, dir->i_ino); |
| 352 | nm.name = NULL; |
| 353 | dent = ubifs_tnc_next_ent(c, &key, &nm); |
| 354 | if (IS_ERR(dent)) { |
| 355 | err = PTR_ERR(dent); |
| 356 | goto out; |
| 357 | } |
| 358 | |
| 359 | file->f_pos = key_hash_flash(c, &dent->key); |
| 360 | file->private_data = dent; |
| 361 | } |
| 362 | |
| 363 | dent = file->private_data; |
| 364 | if (!dent) { |
| 365 | /* |
| 366 | * The directory was seek'ed to and is now readdir'ed. |
| 367 | * Find the entry corresponding to @file->f_pos or the |
| 368 | * closest one. |
| 369 | */ |
| 370 | dent_key_init_hash(c, &key, dir->i_ino, file->f_pos); |
| 371 | nm.name = NULL; |
| 372 | dent = ubifs_tnc_next_ent(c, &key, &nm); |
| 373 | if (IS_ERR(dent)) { |
| 374 | err = PTR_ERR(dent); |
| 375 | goto out; |
| 376 | } |
| 377 | file->f_pos = key_hash_flash(c, &dent->key); |
| 378 | file->private_data = dent; |
| 379 | } |
| 380 | |
| 381 | while (1) { |
| 382 | dbg_gen("feed '%s', ino %llu, new f_pos %#x", |
| 383 | dent->name, (unsigned long long)le64_to_cpu(dent->inum), |
| 384 | key_hash_flash(c, &dent->key)); |
Patrice Chotard | ec7c77e | 2018-04-27 15:51:23 +0200 | [diff] [blame] | 385 | #ifndef __UBOOT__ |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 386 | ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum); |
Patrice Chotard | ec7c77e | 2018-04-27 15:51:23 +0200 | [diff] [blame] | 387 | #endif |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 388 | |
| 389 | nm.len = le16_to_cpu(dent->nlen); |
| 390 | over = filldir(c, (char *)dent->name, nm.len, |
| 391 | le64_to_cpu(dent->inum), dent->type); |
| 392 | if (over) |
| 393 | return 0; |
| 394 | |
| 395 | /* Switch to the next entry */ |
| 396 | key_read(c, &dent->key, &key); |
| 397 | nm.name = (char *)dent->name; |
| 398 | dent = ubifs_tnc_next_ent(c, &key, &nm); |
| 399 | if (IS_ERR(dent)) { |
| 400 | err = PTR_ERR(dent); |
| 401 | goto out; |
| 402 | } |
| 403 | |
| 404 | kfree(file->private_data); |
| 405 | file->f_pos = key_hash_flash(c, &dent->key); |
| 406 | file->private_data = dent; |
| 407 | cond_resched(); |
| 408 | } |
| 409 | |
| 410 | out: |
| 411 | if (err != -ENOENT) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 412 | ubifs_err(c, "cannot find next direntry, error %d", err); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 413 | return err; |
| 414 | } |
| 415 | |
| 416 | kfree(file->private_data); |
| 417 | file->private_data = NULL; |
| 418 | file->f_pos = 2; |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | static int ubifs_finddir(struct super_block *sb, char *dirname, |
| 423 | unsigned long root_inum, unsigned long *inum) |
| 424 | { |
| 425 | int err; |
| 426 | struct qstr nm; |
| 427 | union ubifs_key key; |
| 428 | struct ubifs_dent_node *dent; |
| 429 | struct ubifs_info *c; |
| 430 | struct file *file; |
| 431 | struct dentry *dentry; |
| 432 | struct inode *dir; |
Stefan Roese | ac3244a | 2012-08-28 14:00:24 +0200 | [diff] [blame] | 433 | int ret = 0; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 434 | |
| 435 | file = kzalloc(sizeof(struct file), 0); |
| 436 | dentry = kzalloc(sizeof(struct dentry), 0); |
| 437 | dir = kzalloc(sizeof(struct inode), 0); |
| 438 | if (!file || !dentry || !dir) { |
| 439 | printf("%s: Error, no memory for malloc!\n", __func__); |
| 440 | err = -ENOMEM; |
| 441 | goto out; |
| 442 | } |
| 443 | |
| 444 | dir->i_sb = sb; |
| 445 | file->f_path.dentry = dentry; |
| 446 | file->f_path.dentry->d_parent = dentry; |
| 447 | file->f_path.dentry->d_inode = dir; |
| 448 | file->f_path.dentry->d_inode->i_ino = root_inum; |
| 449 | c = sb->s_fs_info; |
| 450 | |
| 451 | dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos); |
| 452 | |
| 453 | /* Find the first entry in TNC and save it */ |
| 454 | lowest_dent_key(c, &key, dir->i_ino); |
| 455 | nm.name = NULL; |
| 456 | dent = ubifs_tnc_next_ent(c, &key, &nm); |
| 457 | if (IS_ERR(dent)) { |
| 458 | err = PTR_ERR(dent); |
| 459 | goto out; |
| 460 | } |
| 461 | |
| 462 | file->f_pos = key_hash_flash(c, &dent->key); |
| 463 | file->private_data = dent; |
| 464 | |
| 465 | while (1) { |
| 466 | dbg_gen("feed '%s', ino %llu, new f_pos %#x", |
| 467 | dent->name, (unsigned long long)le64_to_cpu(dent->inum), |
| 468 | key_hash_flash(c, &dent->key)); |
Patrice Chotard | ec7c77e | 2018-04-27 15:51:23 +0200 | [diff] [blame] | 469 | #ifndef __UBOOT__ |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 470 | ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum); |
Patrice Chotard | ec7c77e | 2018-04-27 15:51:23 +0200 | [diff] [blame] | 471 | #endif |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 472 | |
| 473 | nm.len = le16_to_cpu(dent->nlen); |
| 474 | if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) && |
| 475 | (strlen(dirname) == nm.len)) { |
| 476 | *inum = le64_to_cpu(dent->inum); |
Stefan Roese | ac3244a | 2012-08-28 14:00:24 +0200 | [diff] [blame] | 477 | ret = 1; |
| 478 | goto out_free; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* Switch to the next entry */ |
| 482 | key_read(c, &dent->key, &key); |
| 483 | nm.name = (char *)dent->name; |
| 484 | dent = ubifs_tnc_next_ent(c, &key, &nm); |
| 485 | if (IS_ERR(dent)) { |
| 486 | err = PTR_ERR(dent); |
| 487 | goto out; |
| 488 | } |
| 489 | |
| 490 | kfree(file->private_data); |
| 491 | file->f_pos = key_hash_flash(c, &dent->key); |
| 492 | file->private_data = dent; |
| 493 | cond_resched(); |
| 494 | } |
| 495 | |
| 496 | out: |
Stefan Roese | ac3244a | 2012-08-28 14:00:24 +0200 | [diff] [blame] | 497 | if (err != -ENOENT) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 498 | dbg_gen("cannot find next direntry, error %d", err); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 499 | |
Stefan Roese | ac3244a | 2012-08-28 14:00:24 +0200 | [diff] [blame] | 500 | out_free: |
Heinrich Schuchardt | e989279 | 2017-11-08 22:28:47 +0100 | [diff] [blame] | 501 | kfree(file->private_data); |
| 502 | free(file); |
| 503 | free(dentry); |
| 504 | free(dir); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 505 | |
Stefan Roese | ac3244a | 2012-08-28 14:00:24 +0200 | [diff] [blame] | 506 | return ret; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | static unsigned long ubifs_findfile(struct super_block *sb, char *filename) |
| 510 | { |
| 511 | int ret; |
| 512 | char *next; |
| 513 | char fpath[128]; |
Simon Kagstrom | 877501a | 2009-09-15 09:53:29 +0200 | [diff] [blame] | 514 | char symlinkpath[128]; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 515 | char *name = fpath; |
| 516 | unsigned long root_inum = 1; |
| 517 | unsigned long inum; |
Simon Kagstrom | 877501a | 2009-09-15 09:53:29 +0200 | [diff] [blame] | 518 | int symlink_count = 0; /* Don't allow symlink recursion */ |
Ricardo Ribalda Delgado | 4a2e69c | 2010-12-02 15:02:35 +0100 | [diff] [blame] | 519 | char link_name[64]; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 520 | |
| 521 | strcpy(fpath, filename); |
| 522 | |
| 523 | /* Remove all leading slashes */ |
| 524 | while (*name == '/') |
| 525 | name++; |
| 526 | |
| 527 | /* |
| 528 | * Handle root-direcoty ('/') |
| 529 | */ |
| 530 | inum = root_inum; |
| 531 | if (!name || *name == '\0') |
| 532 | return inum; |
| 533 | |
| 534 | for (;;) { |
Simon Kagstrom | 877501a | 2009-09-15 09:53:29 +0200 | [diff] [blame] | 535 | struct inode *inode; |
| 536 | struct ubifs_inode *ui; |
| 537 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 538 | /* Extract the actual part from the pathname. */ |
| 539 | next = strchr(name, '/'); |
| 540 | if (next) { |
| 541 | /* Remove all leading slashes. */ |
| 542 | while (*next == '/') |
| 543 | *(next++) = '\0'; |
| 544 | } |
| 545 | |
| 546 | ret = ubifs_finddir(sb, name, root_inum, &inum); |
Simon Kagstrom | 877501a | 2009-09-15 09:53:29 +0200 | [diff] [blame] | 547 | if (!ret) |
| 548 | return 0; |
| 549 | inode = ubifs_iget(sb, inum); |
| 550 | |
| 551 | if (!inode) |
| 552 | return 0; |
| 553 | ui = ubifs_inode(inode); |
| 554 | |
| 555 | if ((inode->i_mode & S_IFMT) == S_IFLNK) { |
Simon Kagstrom | 877501a | 2009-09-15 09:53:29 +0200 | [diff] [blame] | 556 | char buf[128]; |
| 557 | |
| 558 | /* We have some sort of symlink recursion, bail out */ |
| 559 | if (symlink_count++ > 8) { |
| 560 | printf("Symlink recursion, aborting\n"); |
| 561 | return 0; |
| 562 | } |
| 563 | memcpy(link_name, ui->data, ui->data_len); |
| 564 | link_name[ui->data_len] = '\0'; |
| 565 | |
| 566 | if (link_name[0] == '/') { |
| 567 | /* Absolute path, redo everything without |
| 568 | * the leading slash */ |
| 569 | next = name = link_name + 1; |
| 570 | root_inum = 1; |
| 571 | continue; |
| 572 | } |
| 573 | /* Relative to cur dir */ |
Simon Kagstrom | 8416736 | 2009-09-25 14:05:57 +0200 | [diff] [blame] | 574 | sprintf(buf, "%s/%s", |
Simon Kagstrom | 877501a | 2009-09-15 09:53:29 +0200 | [diff] [blame] | 575 | link_name, next == NULL ? "" : next); |
| 576 | memcpy(symlinkpath, buf, sizeof(buf)); |
| 577 | next = name = symlinkpath; |
| 578 | continue; |
| 579 | } |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 580 | |
| 581 | /* |
| 582 | * Check if directory with this name exists |
| 583 | */ |
| 584 | |
| 585 | /* Found the node! */ |
Simon Kagstrom | 877501a | 2009-09-15 09:53:29 +0200 | [diff] [blame] | 586 | if (!next || *next == '\0') |
| 587 | return inum; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 588 | |
| 589 | root_inum = inum; |
| 590 | name = next; |
| 591 | } |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 596 | int ubifs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info) |
Hans de Goede | b5030b2 | 2015-09-17 18:46:57 -0400 | [diff] [blame] | 597 | { |
| 598 | if (rbdd) { |
| 599 | debug("UBIFS cannot be used with normal block devices\n"); |
| 600 | return -1; |
| 601 | } |
| 602 | |
| 603 | /* |
Simon Glass | e76ee97 | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 604 | * Should never happen since blk_get_device_part_str() already checks |
Hans de Goede | b5030b2 | 2015-09-17 18:46:57 -0400 | [diff] [blame] | 605 | * this, but better safe then sorry. |
| 606 | */ |
| 607 | if (!ubifs_is_mounted()) { |
| 608 | debug("UBIFS not mounted, use ubifsmount to mount volume first!\n"); |
| 609 | return -1; |
| 610 | } |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 615 | int ubifs_ls(const char *filename) |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 616 | { |
| 617 | struct ubifs_info *c = ubifs_sb->s_fs_info; |
| 618 | struct file *file; |
| 619 | struct dentry *dentry; |
| 620 | struct inode *dir; |
| 621 | void *dirent = NULL; |
| 622 | unsigned long inum; |
| 623 | int ret = 0; |
| 624 | |
| 625 | c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY); |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 626 | inum = ubifs_findfile(ubifs_sb, (char *)filename); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 627 | if (!inum) { |
| 628 | ret = -1; |
| 629 | goto out; |
| 630 | } |
| 631 | |
| 632 | file = kzalloc(sizeof(struct file), 0); |
| 633 | dentry = kzalloc(sizeof(struct dentry), 0); |
| 634 | dir = kzalloc(sizeof(struct inode), 0); |
| 635 | if (!file || !dentry || !dir) { |
| 636 | printf("%s: Error, no memory for malloc!\n", __func__); |
| 637 | ret = -ENOMEM; |
| 638 | goto out_mem; |
| 639 | } |
| 640 | |
| 641 | dir->i_sb = ubifs_sb; |
| 642 | file->f_path.dentry = dentry; |
| 643 | file->f_path.dentry->d_parent = dentry; |
| 644 | file->f_path.dentry->d_inode = dir; |
| 645 | file->f_path.dentry->d_inode->i_ino = inum; |
| 646 | file->f_pos = 1; |
| 647 | file->private_data = NULL; |
| 648 | ubifs_printdir(file, dirent); |
| 649 | |
| 650 | out_mem: |
| 651 | if (file) |
| 652 | free(file); |
| 653 | if (dentry) |
| 654 | free(dentry); |
| 655 | if (dir) |
| 656 | free(dir); |
| 657 | |
| 658 | out: |
| 659 | ubi_close_volume(c->ubi); |
| 660 | return ret; |
| 661 | } |
| 662 | |
Hans de Goede | b5030b2 | 2015-09-17 18:46:57 -0400 | [diff] [blame] | 663 | int ubifs_exists(const char *filename) |
| 664 | { |
| 665 | struct ubifs_info *c = ubifs_sb->s_fs_info; |
| 666 | unsigned long inum; |
| 667 | |
| 668 | c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY); |
| 669 | inum = ubifs_findfile(ubifs_sb, (char *)filename); |
| 670 | ubi_close_volume(c->ubi); |
| 671 | |
| 672 | return inum != 0; |
| 673 | } |
| 674 | |
| 675 | int ubifs_size(const char *filename, loff_t *size) |
| 676 | { |
| 677 | struct ubifs_info *c = ubifs_sb->s_fs_info; |
| 678 | unsigned long inum; |
| 679 | struct inode *inode; |
| 680 | int err = 0; |
| 681 | |
| 682 | c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY); |
| 683 | |
| 684 | inum = ubifs_findfile(ubifs_sb, (char *)filename); |
| 685 | if (!inum) { |
| 686 | err = -1; |
| 687 | goto out; |
| 688 | } |
| 689 | |
| 690 | inode = ubifs_iget(ubifs_sb, inum); |
| 691 | if (IS_ERR(inode)) { |
| 692 | printf("%s: Error reading inode %ld!\n", __func__, inum); |
| 693 | err = PTR_ERR(inode); |
| 694 | goto out; |
| 695 | } |
| 696 | |
| 697 | *size = inode->i_size; |
| 698 | |
| 699 | ubifs_iput(inode); |
| 700 | out: |
| 701 | ubi_close_volume(c->ubi); |
| 702 | return err; |
| 703 | } |
| 704 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 705 | /* |
| 706 | * ubifsload... |
| 707 | */ |
| 708 | |
| 709 | /* file.c */ |
| 710 | |
| 711 | static inline void *kmap(struct page *page) |
| 712 | { |
| 713 | return page->addr; |
| 714 | } |
| 715 | |
| 716 | static int read_block(struct inode *inode, void *addr, unsigned int block, |
| 717 | struct ubifs_data_node *dn) |
| 718 | { |
| 719 | struct ubifs_info *c = inode->i_sb->s_fs_info; |
| 720 | int err, len, out_len; |
| 721 | union ubifs_key key; |
| 722 | unsigned int dlen; |
| 723 | |
| 724 | data_key_init(c, &key, inode->i_ino, block); |
| 725 | err = ubifs_tnc_lookup(c, &key, dn); |
| 726 | if (err) { |
| 727 | if (err == -ENOENT) |
| 728 | /* Not found, so it must be a hole */ |
| 729 | memset(addr, 0, UBIFS_BLOCK_SIZE); |
| 730 | return err; |
| 731 | } |
| 732 | |
| 733 | ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum); |
| 734 | |
| 735 | len = le32_to_cpu(dn->size); |
| 736 | if (len <= 0 || len > UBIFS_BLOCK_SIZE) |
| 737 | goto dump; |
| 738 | |
| 739 | dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ; |
| 740 | out_len = UBIFS_BLOCK_SIZE; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 741 | err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len, |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 742 | le16_to_cpu(dn->compr_type)); |
| 743 | if (err || len != out_len) |
| 744 | goto dump; |
| 745 | |
| 746 | /* |
| 747 | * Data length can be less than a full block, even for blocks that are |
| 748 | * not the last in the file (e.g., as a result of making a hole and |
| 749 | * appending data). Ensure that the remainder is zeroed out. |
| 750 | */ |
| 751 | if (len < UBIFS_BLOCK_SIZE) |
| 752 | memset(addr + len, 0, UBIFS_BLOCK_SIZE - len); |
| 753 | |
| 754 | return 0; |
| 755 | |
| 756 | dump: |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 757 | ubifs_err(c, "bad data node (block %u, inode %lu)", |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 758 | block, inode->i_ino); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 759 | ubifs_dump_node(c, dn); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 760 | return -EINVAL; |
| 761 | } |
| 762 | |
Stefan Roese | 28ea29f | 2010-11-01 17:28:00 +0100 | [diff] [blame] | 763 | static int do_readpage(struct ubifs_info *c, struct inode *inode, |
| 764 | struct page *page, int last_block_size) |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 765 | { |
| 766 | void *addr; |
| 767 | int err = 0, i; |
| 768 | unsigned int block, beyond; |
| 769 | struct ubifs_data_node *dn; |
| 770 | loff_t i_size = inode->i_size; |
| 771 | |
| 772 | dbg_gen("ino %lu, pg %lu, i_size %lld", |
| 773 | inode->i_ino, page->index, i_size); |
| 774 | |
| 775 | addr = kmap(page); |
| 776 | |
| 777 | block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT; |
| 778 | beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT; |
| 779 | if (block >= beyond) { |
| 780 | /* Reading beyond inode */ |
| 781 | memset(addr, 0, PAGE_CACHE_SIZE); |
| 782 | goto out; |
| 783 | } |
| 784 | |
| 785 | dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS); |
Daniel Mack | b577580 | 2009-06-04 19:44:12 +0200 | [diff] [blame] | 786 | if (!dn) |
| 787 | return -ENOMEM; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 788 | |
| 789 | i = 0; |
| 790 | while (1) { |
| 791 | int ret; |
| 792 | |
| 793 | if (block >= beyond) { |
| 794 | /* Reading beyond inode */ |
| 795 | err = -ENOENT; |
| 796 | memset(addr, 0, UBIFS_BLOCK_SIZE); |
| 797 | } else { |
Stefan Roese | 28ea29f | 2010-11-01 17:28:00 +0100 | [diff] [blame] | 798 | /* |
| 799 | * Reading last block? Make sure to not write beyond |
| 800 | * the requested size in the destination buffer. |
| 801 | */ |
| 802 | if (((block + 1) == beyond) || last_block_size) { |
| 803 | void *buff; |
| 804 | int dlen; |
| 805 | |
| 806 | /* |
| 807 | * We need to buffer the data locally for the |
| 808 | * last block. This is to not pad the |
| 809 | * destination area to a multiple of |
| 810 | * UBIFS_BLOCK_SIZE. |
| 811 | */ |
Marcel Ziswiler | abc574b | 2015-08-18 13:06:37 +0200 | [diff] [blame] | 812 | buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE); |
Stefan Roese | 28ea29f | 2010-11-01 17:28:00 +0100 | [diff] [blame] | 813 | if (!buff) { |
| 814 | printf("%s: Error, malloc fails!\n", |
| 815 | __func__); |
| 816 | err = -ENOMEM; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 817 | break; |
Stefan Roese | 28ea29f | 2010-11-01 17:28:00 +0100 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /* Read block-size into temp buffer */ |
| 821 | ret = read_block(inode, buff, block, dn); |
| 822 | if (ret) { |
| 823 | err = ret; |
| 824 | if (err != -ENOENT) { |
| 825 | free(buff); |
| 826 | break; |
| 827 | } |
| 828 | } |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 829 | |
Stefan Roese | 28ea29f | 2010-11-01 17:28:00 +0100 | [diff] [blame] | 830 | if (last_block_size) |
| 831 | dlen = last_block_size; |
Pali Rohár | 8d2176a | 2022-05-17 22:45:28 +0200 | [diff] [blame] | 832 | else if (ret) |
| 833 | dlen = UBIFS_BLOCK_SIZE; |
Stefan Roese | 28ea29f | 2010-11-01 17:28:00 +0100 | [diff] [blame] | 834 | else |
| 835 | dlen = le32_to_cpu(dn->size); |
| 836 | |
| 837 | /* Now copy required size back to dest */ |
| 838 | memcpy(addr, buff, dlen); |
| 839 | |
| 840 | free(buff); |
| 841 | } else { |
| 842 | ret = read_block(inode, addr, block, dn); |
| 843 | if (ret) { |
| 844 | err = ret; |
| 845 | if (err != -ENOENT) |
| 846 | break; |
| 847 | } |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | if (++i >= UBIFS_BLOCKS_PER_PAGE) |
| 851 | break; |
| 852 | block += 1; |
| 853 | addr += UBIFS_BLOCK_SIZE; |
| 854 | } |
| 855 | if (err) { |
| 856 | if (err == -ENOENT) { |
| 857 | /* Not found, so it must be a hole */ |
| 858 | dbg_gen("hole"); |
| 859 | goto out_free; |
| 860 | } |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 861 | ubifs_err(c, "cannot read page %lu of inode %lu, error %d", |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 862 | page->index, inode->i_ino, err); |
| 863 | goto error; |
| 864 | } |
| 865 | |
| 866 | out_free: |
| 867 | kfree(dn); |
| 868 | out: |
| 869 | return 0; |
| 870 | |
| 871 | error: |
| 872 | kfree(dn); |
| 873 | return err; |
| 874 | } |
| 875 | |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 876 | int ubifs_read(const char *filename, void *buf, loff_t offset, |
| 877 | loff_t size, loff_t *actread) |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 878 | { |
| 879 | struct ubifs_info *c = ubifs_sb->s_fs_info; |
| 880 | unsigned long inum; |
| 881 | struct inode *inode; |
| 882 | struct page page; |
| 883 | int err = 0; |
| 884 | int i; |
| 885 | int count; |
Stefan Roese | 28ea29f | 2010-11-01 17:28:00 +0100 | [diff] [blame] | 886 | int last_block_size = 0; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 887 | |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 888 | *actread = 0; |
| 889 | |
| 890 | if (offset & (PAGE_SIZE - 1)) { |
Vagrant Cascadian | bfdfd10 | 2016-10-23 20:45:16 -0700 | [diff] [blame] | 891 | printf("ubifs: Error offset must be a multiple of %d\n", |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 892 | PAGE_SIZE); |
| 893 | return -1; |
| 894 | } |
| 895 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 896 | c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY); |
Simon Kagstrom | 877501a | 2009-09-15 09:53:29 +0200 | [diff] [blame] | 897 | /* ubifs_findfile will resolve symlinks, so we know that we get |
| 898 | * the real file here */ |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 899 | inum = ubifs_findfile(ubifs_sb, (char *)filename); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 900 | if (!inum) { |
| 901 | err = -1; |
| 902 | goto out; |
| 903 | } |
| 904 | |
| 905 | /* |
| 906 | * Read file inode |
| 907 | */ |
| 908 | inode = ubifs_iget(ubifs_sb, inum); |
| 909 | if (IS_ERR(inode)) { |
| 910 | printf("%s: Error reading inode %ld!\n", __func__, inum); |
| 911 | err = PTR_ERR(inode); |
| 912 | goto out; |
| 913 | } |
| 914 | |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 915 | if (offset > inode->i_size) { |
| 916 | printf("ubifs: Error offset (%lld) > file-size (%lld)\n", |
| 917 | offset, size); |
| 918 | err = -1; |
| 919 | goto put_inode; |
| 920 | } |
| 921 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 922 | /* |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 923 | * If no size was specified or if size bigger than filesize |
| 924 | * set size to filesize |
| 925 | */ |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 926 | if ((size == 0) || (size > (inode->i_size - offset))) |
| 927 | size = inode->i_size - offset; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 928 | |
| 929 | count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 930 | |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 931 | page.addr = buf; |
| 932 | page.index = offset / PAGE_SIZE; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 933 | page.inode = inode; |
| 934 | for (i = 0; i < count; i++) { |
Stefan Roese | 28ea29f | 2010-11-01 17:28:00 +0100 | [diff] [blame] | 935 | /* |
| 936 | * Make sure to not read beyond the requested size |
| 937 | */ |
| 938 | if (((i + 1) == count) && (size < inode->i_size)) |
| 939 | last_block_size = size - (i * PAGE_SIZE); |
| 940 | |
| 941 | err = do_readpage(c, inode, &page, last_block_size); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 942 | if (err) |
| 943 | break; |
| 944 | |
| 945 | page.addr += PAGE_SIZE; |
| 946 | page.index++; |
| 947 | } |
| 948 | |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 949 | if (err) { |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 950 | printf("Error reading file '%s'\n", filename); |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 951 | *actread = i * PAGE_SIZE; |
| 952 | } else { |
| 953 | *actread = size; |
Bastian Ruppert | fd43edd | 2011-09-05 03:03:57 +0000 | [diff] [blame] | 954 | } |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 955 | |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 956 | put_inode: |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 957 | ubifs_iput(inode); |
| 958 | |
| 959 | out: |
| 960 | ubi_close_volume(c->ubi); |
| 961 | return err; |
| 962 | } |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 963 | |
Hans de Goede | b5030b2 | 2015-09-17 18:46:57 -0400 | [diff] [blame] | 964 | void ubifs_close(void) |
| 965 | { |
| 966 | } |
| 967 | |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 968 | /* Compat wrappers for common/cmd_ubifs.c */ |
Ben Dooks | 68ebcf7 | 2023-06-06 09:23:28 +0100 | [diff] [blame] | 969 | int ubifs_load(char *filename, unsigned long addr, u32 size) |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 970 | { |
| 971 | loff_t actread; |
| 972 | int err; |
| 973 | |
Ben Dooks | 68ebcf7 | 2023-06-06 09:23:28 +0100 | [diff] [blame] | 974 | printf("Loading file '%s' to addr 0x%08lx...\n", filename, addr); |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 975 | |
Siva Durga Prasad Paladugu | 67eccfb | 2017-05-30 14:29:06 +0200 | [diff] [blame] | 976 | err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread); |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 977 | if (err == 0) { |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 978 | env_set_hex("filesize", actread); |
Hans de Goede | a164477 | 2015-09-17 18:46:56 -0400 | [diff] [blame] | 979 | printf("Done\n"); |
| 980 | } |
| 981 | |
| 982 | return err; |
| 983 | } |
| 984 | |
| 985 | void uboot_ubifs_umount(void) |
| 986 | { |
| 987 | if (ubifs_sb) { |
| 988 | printf("Unmounting UBIFS volume %s!\n", |
| 989 | ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name); |
| 990 | ubifs_umount(ubifs_sb->s_fs_info); |
| 991 | ubifs_sb = NULL; |
| 992 | } |
| 993 | } |