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