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