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