blob: 49e6f469ca6c47d3b515dc2ad7308008c045ab39 [file] [log] [blame]
Stefan Roese2fc10f62009-03-19 15:35:05 +01001/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
Stefan Roese28ea29f2010-11-01 17:28:00 +01006 * (C) Copyright 2008-2010
Stefan Roese2fc10f62009-03-19 15:35:05 +01007 * Stefan Roese, DENX Software Engineering, sr@denx.de.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 51
20 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * Authors: Artem Bityutskiy (Битюцкий Артём)
23 * Adrian Hunter
24 */
25
26#include "ubifs.h"
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020027#include <u-boot/zlib.h>
Stefan Roese2fc10f62009-03-19 15:35:05 +010028
Heiko Schocherf5895d12014-06-24 10:10:04 +020029#include <linux/err.h>
30#include <linux/lzo.h>
31
Stefan Roese2fc10f62009-03-19 15:35:05 +010032DECLARE_GLOBAL_DATA_PTR;
33
34/* compress.c */
35
36/*
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020037 * We need a wrapper for zunzip() because the parameters are
Stefan Roese2fc10f62009-03-19 15:35:05 +010038 * incompatible with the lzo decompressor.
39 */
40static int gzip_decompress(const unsigned char *in, size_t in_len,
41 unsigned char *out, size_t *out_len)
42{
Veli-Pekka Peltola7b7e1552012-09-05 18:05:14 +030043 return zunzip(out, *out_len, (unsigned char *)in,
44 (unsigned long *)out_len, 0, 0);
Stefan Roese2fc10f62009-03-19 15:35:05 +010045}
46
47/* Fake description object for the "none" compressor */
48static struct ubifs_compressor none_compr = {
49 .compr_type = UBIFS_COMPR_NONE,
Heiko Schocherf5895d12014-06-24 10:10:04 +020050 .name = "none",
Stefan Roese2fc10f62009-03-19 15:35:05 +010051 .capi_name = "",
52 .decompress = NULL,
53};
54
55static struct ubifs_compressor lzo_compr = {
56 .compr_type = UBIFS_COMPR_LZO,
Heiko Schocherf5895d12014-06-24 10:10:04 +020057#ifndef __UBOOT__
58 .comp_mutex = &lzo_mutex,
59#endif
60 .name = "lzo",
Stefan Roese2fc10f62009-03-19 15:35:05 +010061 .capi_name = "lzo",
62 .decompress = lzo1x_decompress_safe,
63};
64
65static struct ubifs_compressor zlib_compr = {
66 .compr_type = UBIFS_COMPR_ZLIB,
Heiko Schocherf5895d12014-06-24 10:10:04 +020067#ifndef __UBOOT__
68 .comp_mutex = &deflate_mutex,
69 .decomp_mutex = &inflate_mutex,
70#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +010071 .name = "zlib",
72 .capi_name = "deflate",
73 .decompress = gzip_decompress,
74};
75
76/* All UBIFS compressors */
77struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
78
Heiko Schocherf5895d12014-06-24 10:10:04 +020079
80#ifdef __UBOOT__
81/* from mm/util.c */
82
83/**
84 * kmemdup - duplicate region of memory
85 *
86 * @src: memory region to duplicate
87 * @len: memory region length
88 * @gfp: GFP mask to use
89 */
90void *kmemdup(const void *src, size_t len, gfp_t gfp)
91{
92 void *p;
93
94 p = kmalloc(len, gfp);
95 if (p)
96 memcpy(p, src, len);
97 return p;
98}
99
100struct crypto_comp {
101 int compressor;
102};
103
104static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
105 u32 type, u32 mask)
106{
107 struct ubifs_compressor *comp;
108 struct crypto_comp *ptr;
109 int i = 0;
110
111 ptr = malloc(sizeof(struct crypto_comp));
112 while (i < UBIFS_COMPR_TYPES_CNT) {
113 comp = ubifs_compressors[i];
114 if (!comp) {
115 i++;
116 continue;
117 }
118 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
119 ptr->compressor = i;
120 return ptr;
121 }
122 i++;
123 }
124 if (i >= UBIFS_COMPR_TYPES_CNT) {
125 ubifs_err("invalid compression type %s", alg_name);
126 free (ptr);
127 return NULL;
128 }
129 return ptr;
130}
131static inline int crypto_comp_decompress(struct crypto_comp *tfm,
132 const u8 *src, unsigned int slen,
133 u8 *dst, unsigned int *dlen)
134{
135 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
136 int err;
137
138 if (compr->compr_type == UBIFS_COMPR_NONE) {
139 memcpy(dst, src, slen);
140 *dlen = slen;
141 return 0;
142 }
143
144 err = compr->decompress(src, slen, dst, (size_t *)dlen);
145 if (err)
146 ubifs_err("cannot decompress %d bytes, compressor %s, "
147 "error %d", slen, compr->name, err);
148
149 return err;
150
151 return 0;
152}
153#endif
154
Stefan Roese2fc10f62009-03-19 15:35:05 +0100155/**
156 * ubifs_decompress - decompress data.
157 * @in_buf: data to decompress
158 * @in_len: length of the data to decompress
159 * @out_buf: output buffer where decompressed data should
160 * @out_len: output length is returned here
161 * @compr_type: type of compression
162 *
163 * This function decompresses data from buffer @in_buf into buffer @out_buf.
164 * The length of the uncompressed data is returned in @out_len. This functions
165 * returns %0 on success or a negative error code on failure.
166 */
167int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
168 int *out_len, int compr_type)
169{
170 int err;
171 struct ubifs_compressor *compr;
172
173 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
174 ubifs_err("invalid compression type %d", compr_type);
175 return -EINVAL;
176 }
177
178 compr = ubifs_compressors[compr_type];
179
180 if (unlikely(!compr->capi_name)) {
181 ubifs_err("%s compression is not compiled in", compr->name);
182 return -EINVAL;
183 }
184
185 if (compr_type == UBIFS_COMPR_NONE) {
186 memcpy(out_buf, in_buf, in_len);
187 *out_len = in_len;
188 return 0;
189 }
190
Heiko Schocherf5895d12014-06-24 10:10:04 +0200191 if (compr->decomp_mutex)
192 mutex_lock(compr->decomp_mutex);
193 err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf,
194 (unsigned int *)out_len);
195 if (compr->decomp_mutex)
196 mutex_unlock(compr->decomp_mutex);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100197 if (err)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200198 ubifs_err("cannot decompress %d bytes, compressor %s, error %d",
199 in_len, compr->name, err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100200
201 return err;
202}
203
204/**
205 * compr_init - initialize a compressor.
206 * @compr: compressor description object
207 *
208 * This function initializes the requested compressor and returns zero in case
209 * of success or a negative error code in case of failure.
210 */
211static int __init compr_init(struct ubifs_compressor *compr)
212{
213 ubifs_compressors[compr->compr_type] = compr;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500214
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200215#ifdef CONFIG_NEEDS_MANUAL_RELOC
Stefan Roese2fc10f62009-03-19 15:35:05 +0100216 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
217 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
218 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500219#endif
220
Heiko Schocherf5895d12014-06-24 10:10:04 +0200221 if (compr->capi_name) {
222 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
223 if (IS_ERR(compr->cc)) {
224 ubifs_err("cannot initialize compressor %s, error %ld",
225 compr->name, PTR_ERR(compr->cc));
226 return PTR_ERR(compr->cc);
227 }
228 }
229
Stefan Roese2fc10f62009-03-19 15:35:05 +0100230 return 0;
231}
232
233/**
234 * ubifs_compressors_init - initialize UBIFS compressors.
235 *
236 * This function initializes the compressor which were compiled in. Returns
237 * zero in case of success and a negative error code in case of failure.
238 */
239int __init ubifs_compressors_init(void)
240{
241 int err;
242
243 err = compr_init(&lzo_compr);
244 if (err)
245 return err;
246
247 err = compr_init(&zlib_compr);
248 if (err)
249 return err;
250
Michael Lawnickb0617012009-03-19 10:06:41 +0100251 err = compr_init(&none_compr);
252 if (err)
253 return err;
254
Stefan Roese2fc10f62009-03-19 15:35:05 +0100255 return 0;
256}
257
258/*
259 * ubifsls...
260 */
261
262static int filldir(struct ubifs_info *c, const char *name, int namlen,
263 u64 ino, unsigned int d_type)
264{
265 struct inode *inode;
266 char filetime[32];
267
268 switch (d_type) {
269 case UBIFS_ITYPE_REG:
270 printf("\t");
271 break;
272 case UBIFS_ITYPE_DIR:
273 printf("<DIR>\t");
274 break;
275 case UBIFS_ITYPE_LNK:
276 printf("<LNK>\t");
277 break;
278 default:
279 printf("other\t");
280 break;
281 }
282
283 inode = ubifs_iget(c->vfs_sb, ino);
284 if (IS_ERR(inode)) {
285 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
286 __func__, ino, inode);
287 return -1;
288 }
289 ctime_r((time_t *)&inode->i_mtime, filetime);
290 printf("%9lld %24.24s ", inode->i_size, filetime);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200291#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100292 ubifs_iput(inode);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200293#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100294
295 printf("%s\n", name);
296
297 return 0;
298}
299
300static int ubifs_printdir(struct file *file, void *dirent)
301{
302 int err, over = 0;
303 struct qstr nm;
304 union ubifs_key key;
305 struct ubifs_dent_node *dent;
306 struct inode *dir = file->f_path.dentry->d_inode;
307 struct ubifs_info *c = dir->i_sb->s_fs_info;
308
309 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
310
311 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
312 /*
313 * The directory was seek'ed to a senseless position or there
314 * are no more entries.
315 */
316 return 0;
317
318 if (file->f_pos == 1) {
319 /* Find the first entry in TNC and save it */
320 lowest_dent_key(c, &key, dir->i_ino);
321 nm.name = NULL;
322 dent = ubifs_tnc_next_ent(c, &key, &nm);
323 if (IS_ERR(dent)) {
324 err = PTR_ERR(dent);
325 goto out;
326 }
327
328 file->f_pos = key_hash_flash(c, &dent->key);
329 file->private_data = dent;
330 }
331
332 dent = file->private_data;
333 if (!dent) {
334 /*
335 * The directory was seek'ed to and is now readdir'ed.
336 * Find the entry corresponding to @file->f_pos or the
337 * closest one.
338 */
339 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
340 nm.name = NULL;
341 dent = ubifs_tnc_next_ent(c, &key, &nm);
342 if (IS_ERR(dent)) {
343 err = PTR_ERR(dent);
344 goto out;
345 }
346 file->f_pos = key_hash_flash(c, &dent->key);
347 file->private_data = dent;
348 }
349
350 while (1) {
351 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
352 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
353 key_hash_flash(c, &dent->key));
354 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
355
356 nm.len = le16_to_cpu(dent->nlen);
357 over = filldir(c, (char *)dent->name, nm.len,
358 le64_to_cpu(dent->inum), dent->type);
359 if (over)
360 return 0;
361
362 /* Switch to the next entry */
363 key_read(c, &dent->key, &key);
364 nm.name = (char *)dent->name;
365 dent = ubifs_tnc_next_ent(c, &key, &nm);
366 if (IS_ERR(dent)) {
367 err = PTR_ERR(dent);
368 goto out;
369 }
370
371 kfree(file->private_data);
372 file->f_pos = key_hash_flash(c, &dent->key);
373 file->private_data = dent;
374 cond_resched();
375 }
376
377out:
378 if (err != -ENOENT) {
379 ubifs_err("cannot find next direntry, error %d", err);
380 return err;
381 }
382
383 kfree(file->private_data);
384 file->private_data = NULL;
385 file->f_pos = 2;
386 return 0;
387}
388
389static int ubifs_finddir(struct super_block *sb, char *dirname,
390 unsigned long root_inum, unsigned long *inum)
391{
392 int err;
393 struct qstr nm;
394 union ubifs_key key;
395 struct ubifs_dent_node *dent;
396 struct ubifs_info *c;
397 struct file *file;
398 struct dentry *dentry;
399 struct inode *dir;
Stefan Roeseac3244a2012-08-28 14:00:24 +0200400 int ret = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100401
402 file = kzalloc(sizeof(struct file), 0);
403 dentry = kzalloc(sizeof(struct dentry), 0);
404 dir = kzalloc(sizeof(struct inode), 0);
405 if (!file || !dentry || !dir) {
406 printf("%s: Error, no memory for malloc!\n", __func__);
407 err = -ENOMEM;
408 goto out;
409 }
410
411 dir->i_sb = sb;
412 file->f_path.dentry = dentry;
413 file->f_path.dentry->d_parent = dentry;
414 file->f_path.dentry->d_inode = dir;
415 file->f_path.dentry->d_inode->i_ino = root_inum;
416 c = sb->s_fs_info;
417
418 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
419
420 /* Find the first entry in TNC and save it */
421 lowest_dent_key(c, &key, dir->i_ino);
422 nm.name = NULL;
423 dent = ubifs_tnc_next_ent(c, &key, &nm);
424 if (IS_ERR(dent)) {
425 err = PTR_ERR(dent);
426 goto out;
427 }
428
429 file->f_pos = key_hash_flash(c, &dent->key);
430 file->private_data = dent;
431
432 while (1) {
433 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
434 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
435 key_hash_flash(c, &dent->key));
436 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
437
438 nm.len = le16_to_cpu(dent->nlen);
439 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
440 (strlen(dirname) == nm.len)) {
441 *inum = le64_to_cpu(dent->inum);
Stefan Roeseac3244a2012-08-28 14:00:24 +0200442 ret = 1;
443 goto out_free;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100444 }
445
446 /* Switch to the next entry */
447 key_read(c, &dent->key, &key);
448 nm.name = (char *)dent->name;
449 dent = ubifs_tnc_next_ent(c, &key, &nm);
450 if (IS_ERR(dent)) {
451 err = PTR_ERR(dent);
452 goto out;
453 }
454
455 kfree(file->private_data);
456 file->f_pos = key_hash_flash(c, &dent->key);
457 file->private_data = dent;
458 cond_resched();
459 }
460
461out:
Stefan Roeseac3244a2012-08-28 14:00:24 +0200462 if (err != -ENOENT)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100463 ubifs_err("cannot find next direntry, error %d", err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100464
Stefan Roeseac3244a2012-08-28 14:00:24 +0200465out_free:
Wolfgang Denkf9b3a472011-07-28 15:27:22 +0200466 if (file->private_data)
467 kfree(file->private_data);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100468 if (file)
469 free(file);
470 if (dentry)
471 free(dentry);
472 if (dir)
473 free(dir);
474
Stefan Roeseac3244a2012-08-28 14:00:24 +0200475 return ret;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100476}
477
478static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
479{
480 int ret;
481 char *next;
482 char fpath[128];
Simon Kagstrom877501a2009-09-15 09:53:29 +0200483 char symlinkpath[128];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100484 char *name = fpath;
485 unsigned long root_inum = 1;
486 unsigned long inum;
Simon Kagstrom877501a2009-09-15 09:53:29 +0200487 int symlink_count = 0; /* Don't allow symlink recursion */
Ricardo Ribalda Delgado4a2e69c2010-12-02 15:02:35 +0100488 char link_name[64];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100489
490 strcpy(fpath, filename);
491
492 /* Remove all leading slashes */
493 while (*name == '/')
494 name++;
495
496 /*
497 * Handle root-direcoty ('/')
498 */
499 inum = root_inum;
500 if (!name || *name == '\0')
501 return inum;
502
503 for (;;) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200504 struct inode *inode;
505 struct ubifs_inode *ui;
506
Stefan Roese2fc10f62009-03-19 15:35:05 +0100507 /* Extract the actual part from the pathname. */
508 next = strchr(name, '/');
509 if (next) {
510 /* Remove all leading slashes. */
511 while (*next == '/')
512 *(next++) = '\0';
513 }
514
515 ret = ubifs_finddir(sb, name, root_inum, &inum);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200516 if (!ret)
517 return 0;
518 inode = ubifs_iget(sb, inum);
519
520 if (!inode)
521 return 0;
522 ui = ubifs_inode(inode);
523
524 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200525 char buf[128];
526
527 /* We have some sort of symlink recursion, bail out */
528 if (symlink_count++ > 8) {
529 printf("Symlink recursion, aborting\n");
530 return 0;
531 }
532 memcpy(link_name, ui->data, ui->data_len);
533 link_name[ui->data_len] = '\0';
534
535 if (link_name[0] == '/') {
536 /* Absolute path, redo everything without
537 * the leading slash */
538 next = name = link_name + 1;
539 root_inum = 1;
540 continue;
541 }
542 /* Relative to cur dir */
Simon Kagstrom84167362009-09-25 14:05:57 +0200543 sprintf(buf, "%s/%s",
Simon Kagstrom877501a2009-09-15 09:53:29 +0200544 link_name, next == NULL ? "" : next);
545 memcpy(symlinkpath, buf, sizeof(buf));
546 next = name = symlinkpath;
547 continue;
548 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100549
550 /*
551 * Check if directory with this name exists
552 */
553
554 /* Found the node! */
Simon Kagstrom877501a2009-09-15 09:53:29 +0200555 if (!next || *next == '\0')
556 return inum;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100557
558 root_inum = inum;
559 name = next;
560 }
561
562 return 0;
563}
564
565int ubifs_ls(char *filename)
566{
567 struct ubifs_info *c = ubifs_sb->s_fs_info;
568 struct file *file;
569 struct dentry *dentry;
570 struct inode *dir;
571 void *dirent = NULL;
572 unsigned long inum;
573 int ret = 0;
574
575 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
576 inum = ubifs_findfile(ubifs_sb, filename);
577 if (!inum) {
578 ret = -1;
579 goto out;
580 }
581
582 file = kzalloc(sizeof(struct file), 0);
583 dentry = kzalloc(sizeof(struct dentry), 0);
584 dir = kzalloc(sizeof(struct inode), 0);
585 if (!file || !dentry || !dir) {
586 printf("%s: Error, no memory for malloc!\n", __func__);
587 ret = -ENOMEM;
588 goto out_mem;
589 }
590
591 dir->i_sb = ubifs_sb;
592 file->f_path.dentry = dentry;
593 file->f_path.dentry->d_parent = dentry;
594 file->f_path.dentry->d_inode = dir;
595 file->f_path.dentry->d_inode->i_ino = inum;
596 file->f_pos = 1;
597 file->private_data = NULL;
598 ubifs_printdir(file, dirent);
599
600out_mem:
601 if (file)
602 free(file);
603 if (dentry)
604 free(dentry);
605 if (dir)
606 free(dir);
607
608out:
609 ubi_close_volume(c->ubi);
610 return ret;
611}
612
613/*
614 * ubifsload...
615 */
616
617/* file.c */
618
619static inline void *kmap(struct page *page)
620{
621 return page->addr;
622}
623
624static int read_block(struct inode *inode, void *addr, unsigned int block,
625 struct ubifs_data_node *dn)
626{
627 struct ubifs_info *c = inode->i_sb->s_fs_info;
628 int err, len, out_len;
629 union ubifs_key key;
630 unsigned int dlen;
631
632 data_key_init(c, &key, inode->i_ino, block);
633 err = ubifs_tnc_lookup(c, &key, dn);
634 if (err) {
635 if (err == -ENOENT)
636 /* Not found, so it must be a hole */
637 memset(addr, 0, UBIFS_BLOCK_SIZE);
638 return err;
639 }
640
641 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
642
643 len = le32_to_cpu(dn->size);
644 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
645 goto dump;
646
647 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
648 out_len = UBIFS_BLOCK_SIZE;
649 err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
650 le16_to_cpu(dn->compr_type));
651 if (err || len != out_len)
652 goto dump;
653
654 /*
655 * Data length can be less than a full block, even for blocks that are
656 * not the last in the file (e.g., as a result of making a hole and
657 * appending data). Ensure that the remainder is zeroed out.
658 */
659 if (len < UBIFS_BLOCK_SIZE)
660 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
661
662 return 0;
663
664dump:
665 ubifs_err("bad data node (block %u, inode %lu)",
666 block, inode->i_ino);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200667 ubifs_dump_node(c, dn);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100668 return -EINVAL;
669}
670
Stefan Roese28ea29f2010-11-01 17:28:00 +0100671static int do_readpage(struct ubifs_info *c, struct inode *inode,
672 struct page *page, int last_block_size)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100673{
674 void *addr;
675 int err = 0, i;
676 unsigned int block, beyond;
677 struct ubifs_data_node *dn;
678 loff_t i_size = inode->i_size;
679
680 dbg_gen("ino %lu, pg %lu, i_size %lld",
681 inode->i_ino, page->index, i_size);
682
683 addr = kmap(page);
684
685 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
686 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
687 if (block >= beyond) {
688 /* Reading beyond inode */
689 memset(addr, 0, PAGE_CACHE_SIZE);
690 goto out;
691 }
692
693 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
Daniel Mackb5775802009-06-04 19:44:12 +0200694 if (!dn)
695 return -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100696
697 i = 0;
698 while (1) {
699 int ret;
700
701 if (block >= beyond) {
702 /* Reading beyond inode */
703 err = -ENOENT;
704 memset(addr, 0, UBIFS_BLOCK_SIZE);
705 } else {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100706 /*
707 * Reading last block? Make sure to not write beyond
708 * the requested size in the destination buffer.
709 */
710 if (((block + 1) == beyond) || last_block_size) {
711 void *buff;
712 int dlen;
713
714 /*
715 * We need to buffer the data locally for the
716 * last block. This is to not pad the
717 * destination area to a multiple of
718 * UBIFS_BLOCK_SIZE.
719 */
720 buff = malloc(UBIFS_BLOCK_SIZE);
721 if (!buff) {
722 printf("%s: Error, malloc fails!\n",
723 __func__);
724 err = -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100725 break;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100726 }
727
728 /* Read block-size into temp buffer */
729 ret = read_block(inode, buff, block, dn);
730 if (ret) {
731 err = ret;
732 if (err != -ENOENT) {
733 free(buff);
734 break;
735 }
736 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100737
Stefan Roese28ea29f2010-11-01 17:28:00 +0100738 if (last_block_size)
739 dlen = last_block_size;
740 else
741 dlen = le32_to_cpu(dn->size);
742
743 /* Now copy required size back to dest */
744 memcpy(addr, buff, dlen);
745
746 free(buff);
747 } else {
748 ret = read_block(inode, addr, block, dn);
749 if (ret) {
750 err = ret;
751 if (err != -ENOENT)
752 break;
753 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100754 }
755 }
756 if (++i >= UBIFS_BLOCKS_PER_PAGE)
757 break;
758 block += 1;
759 addr += UBIFS_BLOCK_SIZE;
760 }
761 if (err) {
762 if (err == -ENOENT) {
763 /* Not found, so it must be a hole */
764 dbg_gen("hole");
765 goto out_free;
766 }
767 ubifs_err("cannot read page %lu of inode %lu, error %d",
768 page->index, inode->i_ino, err);
769 goto error;
770 }
771
772out_free:
773 kfree(dn);
774out:
775 return 0;
776
777error:
778 kfree(dn);
779 return err;
780}
781
782int ubifs_load(char *filename, u32 addr, u32 size)
783{
784 struct ubifs_info *c = ubifs_sb->s_fs_info;
785 unsigned long inum;
786 struct inode *inode;
787 struct page page;
788 int err = 0;
789 int i;
790 int count;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100791 int last_block_size = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100792
793 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200794 /* ubifs_findfile will resolve symlinks, so we know that we get
795 * the real file here */
Stefan Roese2fc10f62009-03-19 15:35:05 +0100796 inum = ubifs_findfile(ubifs_sb, filename);
797 if (!inum) {
798 err = -1;
799 goto out;
800 }
801
802 /*
803 * Read file inode
804 */
805 inode = ubifs_iget(ubifs_sb, inum);
806 if (IS_ERR(inode)) {
807 printf("%s: Error reading inode %ld!\n", __func__, inum);
808 err = PTR_ERR(inode);
809 goto out;
810 }
811
812 /*
Stefan Roese2fc10f62009-03-19 15:35:05 +0100813 * If no size was specified or if size bigger than filesize
814 * set size to filesize
815 */
816 if ((size == 0) || (size > inode->i_size))
817 size = inode->i_size;
818
819 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
820 printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
821 filename, addr, size, size);
822
823 page.addr = (void *)addr;
824 page.index = 0;
825 page.inode = inode;
826 for (i = 0; i < count; i++) {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100827 /*
828 * Make sure to not read beyond the requested size
829 */
830 if (((i + 1) == count) && (size < inode->i_size))
831 last_block_size = size - (i * PAGE_SIZE);
832
833 err = do_readpage(c, inode, &page, last_block_size);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100834 if (err)
835 break;
836
837 page.addr += PAGE_SIZE;
838 page.index++;
839 }
840
841 if (err)
842 printf("Error reading file '%s'\n", filename);
Bastian Ruppertfd43edd2011-09-05 03:03:57 +0000843 else {
Simon Glassfff5c072013-02-24 17:33:23 +0000844 setenv_hex("filesize", size);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100845 printf("Done\n");
Bastian Ruppertfd43edd2011-09-05 03:03:57 +0000846 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100847
848 ubifs_iput(inode);
849
850out:
851 ubi_close_volume(c->ubi);
852 return err;
853}