blob: 835dc5d28055d911cfe7a5d86e28612bece0cc1a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/crc32.h>
15#include <linux/pagemap.h>
16#include <linux/mtd/mtd.h>
17#include <linux/compiler.h>
18#include "nodelist.h"
19#include "compr.h"
20
21int jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
22 struct jffs2_full_dnode *fd, unsigned char *buf,
23 int ofs, int len)
24{
25 struct jffs2_raw_inode *ri;
26 size_t readlen;
27 uint32_t crc;
28 unsigned char *decomprbuf = NULL;
29 unsigned char *readbuf = NULL;
30 int ret = 0;
31
32 ri = jffs2_alloc_raw_inode();
33 if (!ri)
34 return -ENOMEM;
35
36 ret = jffs2_flash_read(c, ref_offset(fd->raw), sizeof(*ri), &readlen, (char *)ri);
37 if (ret) {
38 jffs2_free_raw_inode(ri);
39 printk(KERN_WARNING "Error reading node from 0x%08x: %d\n", ref_offset(fd->raw), ret);
40 return ret;
41 }
42 if (readlen != sizeof(*ri)) {
43 jffs2_free_raw_inode(ri);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000044 printk(KERN_WARNING "Short read from 0x%08x: wanted 0x%zx bytes, got 0x%zx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 ref_offset(fd->raw), sizeof(*ri), readlen);
46 return -EIO;
47 }
48 crc = crc32(0, ri, sizeof(*ri)-8);
49
Joe Perches9c261b32012-02-15 15:56:43 -080050 jffs2_dbg(1, "Node read from %08x: node_crc %08x, calculated CRC %08x. dsize %x, csize %x, offset %x, buf %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 ref_offset(fd->raw), je32_to_cpu(ri->node_crc),
52 crc, je32_to_cpu(ri->dsize), je32_to_cpu(ri->csize),
Joe Perches9c261b32012-02-15 15:56:43 -080053 je32_to_cpu(ri->offset), buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (crc != je32_to_cpu(ri->node_crc)) {
55 printk(KERN_WARNING "Node CRC %08x != calculated CRC %08x for node at %08x\n",
56 je32_to_cpu(ri->node_crc), crc, ref_offset(fd->raw));
57 ret = -EIO;
58 goto out_ri;
59 }
60 /* There was a bug where we wrote hole nodes out with csize/dsize
61 swapped. Deal with it */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000062 if (ri->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(ri->dsize) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 je32_to_cpu(ri->csize)) {
64 ri->dsize = ri->csize;
65 ri->csize = cpu_to_je32(0);
66 }
67
68 D1(if(ofs + len > je32_to_cpu(ri->dsize)) {
69 printk(KERN_WARNING "jffs2_read_dnode() asked for %d bytes at %d from %d-byte node\n",
70 len, ofs, je32_to_cpu(ri->dsize));
71 ret = -EINVAL;
72 goto out_ri;
73 });
74
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (ri->compr == JFFS2_COMPR_ZERO) {
77 memset(buf, 0, len);
78 goto out_ri;
79 }
80
81 /* Cases:
82 Reading whole node and it's uncompressed - read directly to buffer provided, check CRC.
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000083 Reading whole node and it's compressed - read into comprbuf, check CRC and decompress to buffer provided
84 Reading partial node and it's uncompressed - read into readbuf, check CRC, and copy
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 Reading partial node and it's compressed - read into readbuf, check checksum, decompress to decomprbuf and copy
86 */
87 if (ri->compr == JFFS2_COMPR_NONE && len == je32_to_cpu(ri->dsize)) {
88 readbuf = buf;
89 } else {
90 readbuf = kmalloc(je32_to_cpu(ri->csize), GFP_KERNEL);
91 if (!readbuf) {
92 ret = -ENOMEM;
93 goto out_ri;
94 }
95 }
96 if (ri->compr != JFFS2_COMPR_NONE) {
97 if (len < je32_to_cpu(ri->dsize)) {
98 decomprbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
99 if (!decomprbuf) {
100 ret = -ENOMEM;
101 goto out_readbuf;
102 }
103 } else {
104 decomprbuf = buf;
105 }
106 } else {
107 decomprbuf = readbuf;
108 }
109
Joe Perches9c261b32012-02-15 15:56:43 -0800110 jffs2_dbg(2, "Read %d bytes to %p\n", je32_to_cpu(ri->csize),
111 readbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 ret = jffs2_flash_read(c, (ref_offset(fd->raw)) + sizeof(*ri),
113 je32_to_cpu(ri->csize), &readlen, readbuf);
114
115 if (!ret && readlen != je32_to_cpu(ri->csize))
116 ret = -EIO;
117 if (ret)
118 goto out_decomprbuf;
119
120 crc = crc32(0, readbuf, je32_to_cpu(ri->csize));
121 if (crc != je32_to_cpu(ri->data_crc)) {
122 printk(KERN_WARNING "Data CRC %08x != calculated CRC %08x for node at %08x\n",
123 je32_to_cpu(ri->data_crc), crc, ref_offset(fd->raw));
124 ret = -EIO;
125 goto out_decomprbuf;
126 }
Joe Perches9c261b32012-02-15 15:56:43 -0800127 jffs2_dbg(2, "Data CRC matches calculated CRC %08x\n", crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (ri->compr != JFFS2_COMPR_NONE) {
Joe Perches9c261b32012-02-15 15:56:43 -0800129 jffs2_dbg(2, "Decompress %d bytes from %p to %d bytes at %p\n",
130 je32_to_cpu(ri->csize), readbuf,
131 je32_to_cpu(ri->dsize), decomprbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 ret = jffs2_decompress(c, f, ri->compr | (ri->usercompr << 8), readbuf, decomprbuf, je32_to_cpu(ri->csize), je32_to_cpu(ri->dsize));
133 if (ret) {
134 printk(KERN_WARNING "Error: jffs2_decompress returned %d\n", ret);
135 goto out_decomprbuf;
136 }
137 }
138
139 if (len < je32_to_cpu(ri->dsize)) {
140 memcpy(buf, decomprbuf+ofs, len);
141 }
142 out_decomprbuf:
143 if(decomprbuf != buf && decomprbuf != readbuf)
144 kfree(decomprbuf);
145 out_readbuf:
146 if(readbuf != buf)
147 kfree(readbuf);
148 out_ri:
149 jffs2_free_raw_inode(ri);
150
151 return ret;
152}
153
154int jffs2_read_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
155 unsigned char *buf, uint32_t offset, uint32_t len)
156{
157 uint32_t end = offset + len;
158 struct jffs2_node_frag *frag;
159 int ret;
160
Joe Perches9c261b32012-02-15 15:56:43 -0800161 jffs2_dbg(1, "%s(): ino #%u, range 0x%08x-0x%08x\n",
162 __func__, f->inocache->ino, offset, offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 frag = jffs2_lookup_node_frag(&f->fragtree, offset);
165
166 /* XXX FIXME: Where a single physical node actually shows up in two
167 frags, we read it twice. Don't do that. */
David Woodhouse199bc9f2009-11-30 09:06:40 +0000168 /* Now we're pointing at the first frag which overlaps our page
169 * (or perhaps is before it, if we've been asked to read off the
170 * end of the file). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 while(offset < end) {
Joe Perches9c261b32012-02-15 15:56:43 -0800172 jffs2_dbg(2, "%s(): offset %d, end %d\n",
173 __func__, offset, end);
David Woodhouse199bc9f2009-11-30 09:06:40 +0000174 if (unlikely(!frag || frag->ofs > offset ||
175 frag->ofs + frag->size <= offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 uint32_t holesize = end - offset;
David Woodhouse199bc9f2009-11-30 09:06:40 +0000177 if (frag && frag->ofs > offset) {
Joe Perches9c261b32012-02-15 15:56:43 -0800178 jffs2_dbg(1, "Eep. Hole in ino #%u fraglist. frag->ofs = 0x%08x, offset = 0x%08x\n",
179 f->inocache->ino, frag->ofs, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 holesize = min(holesize, frag->ofs - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Joe Perches9c261b32012-02-15 15:56:43 -0800182 jffs2_dbg(1, "Filling non-frag hole from %d-%d\n",
183 offset, offset + holesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 memset(buf, 0, holesize);
185 buf += holesize;
186 offset += holesize;
187 continue;
188 } else if (unlikely(!frag->node)) {
189 uint32_t holeend = min(end, frag->ofs + frag->size);
Joe Perches9c261b32012-02-15 15:56:43 -0800190 jffs2_dbg(1, "Filling frag hole from %d-%d (frag 0x%x 0x%x)\n",
191 offset, holeend, frag->ofs,
192 frag->ofs + frag->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 memset(buf, 0, holeend - offset);
194 buf += holeend - offset;
195 offset = holeend;
196 frag = frag_next(frag);
197 continue;
198 } else {
199 uint32_t readlen;
200 uint32_t fragofs; /* offset within the frag to start reading */
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 fragofs = offset - frag->ofs;
203 readlen = min(frag->size - fragofs, end - offset);
Joe Perches9c261b32012-02-15 15:56:43 -0800204 jffs2_dbg(1, "Reading %d-%d from node at 0x%08x (%d)\n",
205 frag->ofs+fragofs,
206 frag->ofs + fragofs+readlen,
207 ref_offset(frag->node->raw),
208 ref_flags(frag->node->raw));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ret = jffs2_read_dnode(c, f, frag->node, buf, fragofs + frag->ofs - frag->node->ofs, readlen);
Joe Perches9c261b32012-02-15 15:56:43 -0800210 jffs2_dbg(2, "node read done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (ret) {
Joe Perches9c261b32012-02-15 15:56:43 -0800212 jffs2_dbg(1, "%s(): error %d\n",
213 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 memset(buf, 0, readlen);
215 return ret;
216 }
217 buf += readlen;
218 offset += readlen;
219 frag = frag_next(frag);
Joe Perches9c261b32012-02-15 15:56:43 -0800220 jffs2_dbg(2, "node read was OK. Looping\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222 }
223 return 0;
224}
225