2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
8 * The original JFFS, from which the design for JFFS2 was derived,
9 * was designed and implemented by Axis Communications AB.
11 * The contents of this file are subject to the Red Hat eCos Public
12 * License Version 1.1 (the "Licence"); you may not use this file
13 * except in compliance with the Licence. You may obtain a copy of
14 * the Licence at http://www.redhat.com/
16 * Software distributed under the Licence is distributed on an "AS IS"
17 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18 * See the Licence for the specific language governing rights and
19 * limitations under the Licence.
21 * The Original Code is JFFS2 - Journalling Flash File System, version 2
23 * Alternatively, the contents of this file may be used under the
24 * terms of the GNU General Public License version 2 (the "GPL"), in
25 * which case the provisions of the GPL are applicable instead of the
26 * above. If you wish to allow the use of your version of this file
27 * only under the terms of the GPL and not to allow others to use your
28 * version of this file under the RHEPL, indicate your decision by
29 * deleting the provisions above and replace them with the notice and
30 * other provisions required by the GPL. If you do not delete the
31 * provisions above, a recipient may use your version of this file
32 * under either the RHEPL or the GPL.
34 * $Id: read.c,v 1.13.2.2 2003/11/02 13:51:18 dwmw2 Exp $
38 #include <linux/kernel.h>
39 #include <linux/slab.h>
40 #include <linux/jffs2.h>
41 #include <linux/mtd/mtd.h>
43 #include <linux/crc32.h>
45 int jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_full_dnode *fd, unsigned char *buf, int ofs, int len)
47 struct jffs2_raw_inode *ri;
50 unsigned char *decomprbuf = NULL;
51 unsigned char *readbuf = NULL;
54 ri = jffs2_alloc_raw_inode();
58 ret = c->mtd->read(c->mtd, fd->raw->flash_offset & ~3, sizeof(*ri), &readlen, (char *)ri);
60 jffs2_free_raw_inode(ri);
61 printk(KERN_WARNING "Error reading node from 0x%08x: %d\n", fd->raw->flash_offset & ~3, ret);
64 if (readlen != sizeof(*ri)) {
65 jffs2_free_raw_inode(ri);
66 printk(KERN_WARNING "Short read from 0x%08x: wanted 0x%x bytes, got 0x%x\n",
67 fd->raw->flash_offset & ~3, sizeof(*ri), readlen);
70 crc = crc32(0, ri, sizeof(*ri)-8);
72 D1(printk(KERN_DEBUG "Node read from %08x: node_crc %08x, calculated CRC %08x. dsize %x, csize %x, offset %x, buf %p\n", fd->raw->flash_offset & ~3, ri->node_crc, crc, ri->dsize, ri->csize, ri->offset, buf));
73 if (crc != ri->node_crc) {
74 printk(KERN_WARNING "Node CRC %08x != calculated CRC %08x for node at %08x\n", ri->node_crc, crc, fd->raw->flash_offset & ~3);
78 /* There was a bug where we wrote hole nodes out with csize/dsize
79 swapped. Deal with it */
80 if (ri->compr == JFFS2_COMPR_ZERO && !ri->dsize && ri->csize) {
81 ri->dsize = ri->csize;
85 D1(if(ofs + len > ri->dsize) {
86 printk(KERN_WARNING "jffs2_read_dnode() asked for %d bytes at %d from %d-byte node\n", len, ofs, ri->dsize);
92 if (ri->compr == JFFS2_COMPR_ZERO) {
98 Reading whole node and it's uncompressed - read directly to buffer provided, check CRC.
99 Reading whole node and it's compressed - read into comprbuf, check CRC and decompress to buffer provided
100 Reading partial node and it's uncompressed - read into readbuf, check CRC, and copy
101 Reading partial node and it's compressed - read into readbuf, check checksum, decompress to decomprbuf and copy
103 if (ri->compr == JFFS2_COMPR_NONE && len == ri->dsize) {
106 readbuf = kmalloc(ri->csize, GFP_KERNEL);
112 if (ri->compr != JFFS2_COMPR_NONE) {
113 if (len < ri->dsize) {
114 decomprbuf = kmalloc(ri->dsize, GFP_KERNEL);
123 decomprbuf = readbuf;
126 D2(printk(KERN_DEBUG "Read %d bytes to %p\n", ri->csize, readbuf));
127 ret = c->mtd->read(c->mtd, (fd->raw->flash_offset &~3) + sizeof(*ri), ri->csize, &readlen, readbuf);
129 if (!ret && readlen != ri->csize)
134 crc = crc32(0, readbuf, ri->csize);
135 if (crc != ri->data_crc) {
136 printk(KERN_WARNING "Data CRC %08x != calculated CRC %08x for node at %08x\n", ri->data_crc, crc, fd->raw->flash_offset & ~3);
140 D2(printk(KERN_DEBUG "Data CRC matches calculated CRC %08x\n", crc));
141 if (ri->compr != JFFS2_COMPR_NONE) {
142 D2(printk(KERN_DEBUG "Decompress %d bytes from %p to %d bytes at %p\n", ri->csize, readbuf, ri->dsize, decomprbuf));
143 ret = jffs2_decompress(ri->compr, readbuf, decomprbuf, ri->csize, ri->dsize);
145 printk(KERN_WARNING "Error: jffs2_decompress returned %d\n", ret);
150 if (len < ri->dsize) {
151 memcpy(buf, decomprbuf+ofs, len);
154 if(decomprbuf != buf && decomprbuf != readbuf)
160 jffs2_free_raw_inode(ri);