]>
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> |
336d4615 | 17 | #include <malloc.h> |
6e295186 | 18 | #include <memalign.h> |
9eefe2a2 | 19 | #include "ubifs.h" |
61b29b82 | 20 | #include <dm/devres.h> |
c1a0fd5f | 21 | #include <u-boot/zlib.h> |
9eefe2a2 | 22 | |
4839e868 | 23 | #include <linux/compat.h> |
ff94bc40 HS |
24 | #include <linux/err.h> |
25 | #include <linux/lzo.h> | |
26 | ||
9eefe2a2 SR |
27 | DECLARE_GLOBAL_DATA_PTR; |
28 | ||
29 | /* compress.c */ | |
30 | ||
31 | /* | |
c1a0fd5f | 32 | * We need a wrapper for zunzip() because the parameters are |
9eefe2a2 SR |
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 | { | |
8044c138 VPP |
38 | return zunzip(out, *out_len, (unsigned char *)in, |
39 | (unsigned long *)out_len, 0, 0); | |
9eefe2a2 SR |
40 | } |
41 | ||
42 | /* Fake description object for the "none" compressor */ | |
43 | static struct ubifs_compressor none_compr = { | |
44 | .compr_type = UBIFS_COMPR_NONE, | |
ff94bc40 | 45 | .name = "none", |
9eefe2a2 SR |
46 | .capi_name = "", |
47 | .decompress = NULL, | |
48 | }; | |
49 | ||
50 | static struct ubifs_compressor lzo_compr = { | |
51 | .compr_type = UBIFS_COMPR_LZO, | |
ff94bc40 HS |
52 | #ifndef __UBOOT__ |
53 | .comp_mutex = &lzo_mutex, | |
54 | #endif | |
55 | .name = "lzo", | |
9eefe2a2 SR |
56 | .capi_name = "lzo", |
57 | .decompress = lzo1x_decompress_safe, | |
58 | }; | |
59 | ||
60 | static struct ubifs_compressor zlib_compr = { | |
61 | .compr_type = UBIFS_COMPR_ZLIB, | |
ff94bc40 HS |
62 | #ifndef __UBOOT__ |
63 | .comp_mutex = &deflate_mutex, | |
64 | .decomp_mutex = &inflate_mutex, | |
65 | #endif | |
9eefe2a2 SR |
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 | ||
ff94bc40 HS |
74 | |
75 | #ifdef __UBOOT__ | |
ff94bc40 HS |
76 | |
77 | struct crypto_comp { | |
78 | int compressor; | |
79 | }; | |
80 | ||
0195a7bb HS |
81 | static inline struct crypto_comp |
82 | *crypto_alloc_comp(const char *alg_name, u32 type, u32 mask) | |
ff94bc40 HS |
83 | { |
84 | struct ubifs_compressor *comp; | |
85 | struct crypto_comp *ptr; | |
86 | int i = 0; | |
87 | ||
4519668b | 88 | ptr = malloc_cache_aligned(sizeof(struct crypto_comp)); |
ff94bc40 HS |
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) { | |
0195a7bb | 102 | dbg_gen("invalid compression type %s", alg_name); |
ff94bc40 HS |
103 | free (ptr); |
104 | return NULL; | |
105 | } | |
106 | return ptr; | |
107 | } | |
0195a7bb HS |
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) | |
ff94bc40 HS |
112 | { |
113 | struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor]; | |
114 | int err; | |
e4aa10ba | 115 | size_t tmp_len = *dlen; |
ff94bc40 HS |
116 | |
117 | if (compr->compr_type == UBIFS_COMPR_NONE) { | |
118 | memcpy(dst, src, slen); | |
119 | *dlen = slen; | |
120 | return 0; | |
121 | } | |
122 | ||
e4aa10ba | 123 | err = compr->decompress(src, slen, dst, &tmp_len); |
ff94bc40 | 124 | if (err) |
0195a7bb | 125 | ubifs_err(c, "cannot decompress %d bytes, compressor %s, " |
ff94bc40 HS |
126 | "error %d", slen, compr->name, err); |
127 | ||
e4aa10ba | 128 | *dlen = tmp_len; |
ff94bc40 HS |
129 | return err; |
130 | ||
131 | return 0; | |
132 | } | |
dc288431 AH |
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 | ||
ff94bc40 HS |
139 | #endif |
140 | ||
9eefe2a2 SR |
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 | */ | |
0195a7bb HS |
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) | |
9eefe2a2 SR |
155 | { |
156 | int err; | |
157 | struct ubifs_compressor *compr; | |
158 | ||
159 | if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) { | |
0195a7bb | 160 | ubifs_err(c, "invalid compression type %d", compr_type); |
9eefe2a2 SR |
161 | return -EINVAL; |
162 | } | |
163 | ||
164 | compr = ubifs_compressors[compr_type]; | |
165 | ||
166 | if (unlikely(!compr->capi_name)) { | |
0195a7bb | 167 | ubifs_err(c, "%s compression is not compiled in", compr->name); |
9eefe2a2 SR |
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 | ||
ff94bc40 HS |
177 | if (compr->decomp_mutex) |
178 | mutex_lock(compr->decomp_mutex); | |
0195a7bb | 179 | err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf, |
ff94bc40 HS |
180 | (unsigned int *)out_len); |
181 | if (compr->decomp_mutex) | |
182 | mutex_unlock(compr->decomp_mutex); | |
9eefe2a2 | 183 | if (err) |
0195a7bb HS |
184 | ubifs_err(c, "cannot decompress %d bytes, compressor %s," |
185 | " error %d", in_len, compr->name, err); | |
9eefe2a2 SR |
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; | |
521af04d | 200 | |
2e5167cc | 201 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
9eefe2a2 SR |
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; | |
521af04d PT |
205 | #endif |
206 | ||
ff94bc40 HS |
207 | if (compr->capi_name) { |
208 | compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0); | |
209 | if (IS_ERR(compr->cc)) { | |
0195a7bb HS |
210 | dbg_gen("cannot initialize compressor %s," |
211 | " error %ld", compr->name, | |
212 | PTR_ERR(compr->cc)); | |
ff94bc40 HS |
213 | return PTR_ERR(compr->cc); |
214 | } | |
215 | } | |
216 | ||
9eefe2a2 SR |
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 | ||
faac4fd8 ML |
238 | err = compr_init(&none_compr); |
239 | if (err) | |
240 | return err; | |
241 | ||
9eefe2a2 SR |
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); | |
ff94bc40 | 278 | #ifndef __UBOOT__ |
9eefe2a2 | 279 | ubifs_iput(inode); |
ff94bc40 | 280 | #endif |
9eefe2a2 SR |
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)); | |
87deefec | 341 | #ifndef __UBOOT__ |
9eefe2a2 | 342 | ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum); |
87deefec | 343 | #endif |
9eefe2a2 SR |
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) { | |
0195a7bb | 368 | ubifs_err(c, "cannot find next direntry, error %d", err); |
9eefe2a2 SR |
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; | |
be73913b | 389 | int ret = 0; |
9eefe2a2 SR |
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)); | |
87deefec | 425 | #ifndef __UBOOT__ |
9eefe2a2 | 426 | ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum); |
87deefec | 427 | #endif |
9eefe2a2 SR |
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); | |
be73913b SR |
433 | ret = 1; |
434 | goto out_free; | |
9eefe2a2 SR |
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: | |
be73913b | 453 | if (err != -ENOENT) |
0195a7bb | 454 | dbg_gen("cannot find next direntry, error %d", err); |
9eefe2a2 | 455 | |
be73913b | 456 | out_free: |
4b29975f HS |
457 | kfree(file->private_data); |
458 | free(file); | |
459 | free(dentry); | |
460 | free(dir); | |
9eefe2a2 | 461 | |
be73913b | 462 | return ret; |
9eefe2a2 SR |
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]; | |
9d7952e4 | 470 | char symlinkpath[128]; |
9eefe2a2 SR |
471 | char *name = fpath; |
472 | unsigned long root_inum = 1; | |
473 | unsigned long inum; | |
9d7952e4 | 474 | int symlink_count = 0; /* Don't allow symlink recursion */ |
64b68178 | 475 | char link_name[64]; |
9eefe2a2 SR |
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 (;;) { | |
9d7952e4 SK |
491 | struct inode *inode; |
492 | struct ubifs_inode *ui; | |
493 | ||
9eefe2a2 SR |
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); | |
9d7952e4 SK |
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) { | |
9d7952e4 SK |
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 */ | |
ef37c683 | 530 | sprintf(buf, "%s/%s", |
9d7952e4 SK |
531 | link_name, next == NULL ? "" : next); |
532 | memcpy(symlinkpath, buf, sizeof(buf)); | |
533 | next = name = symlinkpath; | |
534 | continue; | |
535 | } | |
9eefe2a2 SR |
536 | |
537 | /* | |
538 | * Check if directory with this name exists | |
539 | */ | |
540 | ||
541 | /* Found the node! */ | |
9d7952e4 SK |
542 | if (!next || *next == '\0') |
543 | return inum; | |
9eefe2a2 SR |
544 | |
545 | root_inum = inum; | |
546 | name = next; | |
547 | } | |
548 | ||
549 | return 0; | |
550 | } | |
551 | ||
4101f687 | 552 | int ubifs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info) |
29cc5bca HG |
553 | { |
554 | if (rbdd) { | |
555 | debug("UBIFS cannot be used with normal block devices\n"); | |
556 | return -1; | |
557 | } | |
558 | ||
559 | /* | |
e35929e4 | 560 | * Should never happen since blk_get_device_part_str() already checks |
29cc5bca HG |
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 | ||
ad15749b | 571 | int ubifs_ls(const char *filename) |
9eefe2a2 SR |
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); | |
ad15749b | 582 | inum = ubifs_findfile(ubifs_sb, (char *)filename); |
9eefe2a2 SR |
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 | ||
29cc5bca HG |
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 | ||
9eefe2a2 SR |
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; | |
0195a7bb | 697 | err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len, |
9eefe2a2 SR |
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: | |
0195a7bb | 713 | ubifs_err(c, "bad data node (block %u, inode %lu)", |
9eefe2a2 | 714 | block, inode->i_ino); |
ff94bc40 | 715 | ubifs_dump_node(c, dn); |
9eefe2a2 SR |
716 | return -EINVAL; |
717 | } | |
718 | ||
b1a14f8a SR |
719 | static int do_readpage(struct ubifs_info *c, struct inode *inode, |
720 | struct page *page, int last_block_size) | |
9eefe2a2 SR |
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); | |
165f9859 DM |
742 | if (!dn) |
743 | return -ENOMEM; | |
9eefe2a2 SR |
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 { | |
b1a14f8a SR |
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 | */ | |
4519668b | 768 | buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE); |
b1a14f8a SR |
769 | if (!buff) { |
770 | printf("%s: Error, malloc fails!\n", | |
771 | __func__); | |
772 | err = -ENOMEM; | |
9eefe2a2 | 773 | break; |
b1a14f8a SR |
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 | } | |
785 | ||
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 | } | |
9eefe2a2 SR |
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 | } | |
0195a7bb | 815 | ubifs_err(c, "cannot read page %lu of inode %lu, error %d", |
9eefe2a2 SR |
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 | ||
ad15749b HG |
830 | int ubifs_read(const char *filename, void *buf, loff_t offset, |
831 | loff_t size, loff_t *actread) | |
9eefe2a2 SR |
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; | |
b1a14f8a | 840 | int last_block_size = 0; |
9eefe2a2 | 841 | |
ad15749b HG |
842 | *actread = 0; |
843 | ||
844 | if (offset & (PAGE_SIZE - 1)) { | |
1381901e | 845 | printf("ubifs: Error offset must be a multiple of %d\n", |
ad15749b HG |
846 | PAGE_SIZE); |
847 | return -1; | |
848 | } | |
849 | ||
9eefe2a2 | 850 | c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY); |
9d7952e4 SK |
851 | /* ubifs_findfile will resolve symlinks, so we know that we get |
852 | * the real file here */ | |
ad15749b | 853 | inum = ubifs_findfile(ubifs_sb, (char *)filename); |
9eefe2a2 SR |
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 | ||
ad15749b HG |
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 | ||
9eefe2a2 SR |
876 | /* |
877 | * If no size was specified or if size bigger than filesize | |
878 | * set size to filesize | |
879 | */ | |
ad15749b HG |
880 | if ((size == 0) || (size > (inode->i_size - offset))) |
881 | size = inode->i_size - offset; | |
9eefe2a2 SR |
882 | |
883 | count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT; | |
9eefe2a2 | 884 | |
ad15749b HG |
885 | page.addr = buf; |
886 | page.index = offset / PAGE_SIZE; | |
9eefe2a2 SR |
887 | page.inode = inode; |
888 | for (i = 0; i < count; i++) { | |
b1a14f8a SR |
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); | |
9eefe2a2 SR |
896 | if (err) |
897 | break; | |
898 | ||
899 | page.addr += PAGE_SIZE; | |
900 | page.index++; | |
901 | } | |
902 | ||
ad15749b | 903 | if (err) { |
9eefe2a2 | 904 | printf("Error reading file '%s'\n", filename); |
ad15749b HG |
905 | *actread = i * PAGE_SIZE; |
906 | } else { | |
907 | *actread = size; | |
46d7274c | 908 | } |
9eefe2a2 | 909 | |
ad15749b | 910 | put_inode: |
9eefe2a2 SR |
911 | ubifs_iput(inode); |
912 | ||
913 | out: | |
914 | ubi_close_volume(c->ubi); | |
915 | return err; | |
916 | } | |
ad15749b | 917 | |
29cc5bca HG |
918 | void ubifs_close(void) |
919 | { | |
920 | } | |
921 | ||
ad15749b HG |
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 | ||
34cc30af | 930 | err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread); |
ad15749b | 931 | if (err == 0) { |
018f5303 | 932 | env_set_hex("filesize", actread); |
ad15749b HG |
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 | } |