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