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