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