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