18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * JFFS2 -- Journalling Flash File System, Version 2. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright © 2001-2007 Red Hat, Inc. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Created by David Woodhouse <dwmw2@infradead.org> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * For licensing information, see the file 'LICENCE' in this directory. 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/kernel.h> 158c2ecf20Sopenharmony_ci#include <linux/slab.h> 168c2ecf20Sopenharmony_ci#include <linux/crc32.h> 178c2ecf20Sopenharmony_ci#include <linux/pagemap.h> 188c2ecf20Sopenharmony_ci#include <linux/mtd/mtd.h> 198c2ecf20Sopenharmony_ci#include <linux/compiler.h> 208c2ecf20Sopenharmony_ci#include "nodelist.h" 218c2ecf20Sopenharmony_ci#include "compr.h" 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ciint jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, 248c2ecf20Sopenharmony_ci struct jffs2_full_dnode *fd, unsigned char *buf, 258c2ecf20Sopenharmony_ci int ofs, int len) 268c2ecf20Sopenharmony_ci{ 278c2ecf20Sopenharmony_ci struct jffs2_raw_inode *ri; 288c2ecf20Sopenharmony_ci size_t readlen; 298c2ecf20Sopenharmony_ci uint32_t crc; 308c2ecf20Sopenharmony_ci unsigned char *decomprbuf = NULL; 318c2ecf20Sopenharmony_ci unsigned char *readbuf = NULL; 328c2ecf20Sopenharmony_ci int ret = 0; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci ri = jffs2_alloc_raw_inode(); 358c2ecf20Sopenharmony_ci if (!ri) 368c2ecf20Sopenharmony_ci return -ENOMEM; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci ret = jffs2_flash_read(c, ref_offset(fd->raw), sizeof(*ri), &readlen, (char *)ri); 398c2ecf20Sopenharmony_ci if (ret) { 408c2ecf20Sopenharmony_ci jffs2_free_raw_inode(ri); 418c2ecf20Sopenharmony_ci pr_warn("Error reading node from 0x%08x: %d\n", 428c2ecf20Sopenharmony_ci ref_offset(fd->raw), ret); 438c2ecf20Sopenharmony_ci return ret; 448c2ecf20Sopenharmony_ci } 458c2ecf20Sopenharmony_ci if (readlen != sizeof(*ri)) { 468c2ecf20Sopenharmony_ci jffs2_free_raw_inode(ri); 478c2ecf20Sopenharmony_ci pr_warn("Short read from 0x%08x: wanted 0x%zx bytes, got 0x%zx\n", 488c2ecf20Sopenharmony_ci ref_offset(fd->raw), sizeof(*ri), readlen); 498c2ecf20Sopenharmony_ci return -EIO; 508c2ecf20Sopenharmony_ci } 518c2ecf20Sopenharmony_ci crc = crc32(0, ri, sizeof(*ri)-8); 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci jffs2_dbg(1, "Node read from %08x: node_crc %08x, calculated CRC %08x. dsize %x, csize %x, offset %x, buf %p\n", 548c2ecf20Sopenharmony_ci ref_offset(fd->raw), je32_to_cpu(ri->node_crc), 558c2ecf20Sopenharmony_ci crc, je32_to_cpu(ri->dsize), je32_to_cpu(ri->csize), 568c2ecf20Sopenharmony_ci je32_to_cpu(ri->offset), buf); 578c2ecf20Sopenharmony_ci if (crc != je32_to_cpu(ri->node_crc)) { 588c2ecf20Sopenharmony_ci pr_warn("Node CRC %08x != calculated CRC %08x for node at %08x\n", 598c2ecf20Sopenharmony_ci je32_to_cpu(ri->node_crc), crc, ref_offset(fd->raw)); 608c2ecf20Sopenharmony_ci ret = -EIO; 618c2ecf20Sopenharmony_ci goto out_ri; 628c2ecf20Sopenharmony_ci } 638c2ecf20Sopenharmony_ci /* There was a bug where we wrote hole nodes out with csize/dsize 648c2ecf20Sopenharmony_ci swapped. Deal with it */ 658c2ecf20Sopenharmony_ci if (ri->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(ri->dsize) && 668c2ecf20Sopenharmony_ci je32_to_cpu(ri->csize)) { 678c2ecf20Sopenharmony_ci ri->dsize = ri->csize; 688c2ecf20Sopenharmony_ci ri->csize = cpu_to_je32(0); 698c2ecf20Sopenharmony_ci } 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci D1(if(ofs + len > je32_to_cpu(ri->dsize)) { 728c2ecf20Sopenharmony_ci pr_warn("jffs2_read_dnode() asked for %d bytes at %d from %d-byte node\n", 738c2ecf20Sopenharmony_ci len, ofs, je32_to_cpu(ri->dsize)); 748c2ecf20Sopenharmony_ci ret = -EINVAL; 758c2ecf20Sopenharmony_ci goto out_ri; 768c2ecf20Sopenharmony_ci }); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci if (ri->compr == JFFS2_COMPR_ZERO) { 808c2ecf20Sopenharmony_ci memset(buf, 0, len); 818c2ecf20Sopenharmony_ci goto out_ri; 828c2ecf20Sopenharmony_ci } 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci /* Cases: 858c2ecf20Sopenharmony_ci Reading whole node and it's uncompressed - read directly to buffer provided, check CRC. 868c2ecf20Sopenharmony_ci Reading whole node and it's compressed - read into comprbuf, check CRC and decompress to buffer provided 878c2ecf20Sopenharmony_ci Reading partial node and it's uncompressed - read into readbuf, check CRC, and copy 888c2ecf20Sopenharmony_ci Reading partial node and it's compressed - read into readbuf, check checksum, decompress to decomprbuf and copy 898c2ecf20Sopenharmony_ci */ 908c2ecf20Sopenharmony_ci if (ri->compr == JFFS2_COMPR_NONE && len == je32_to_cpu(ri->dsize)) { 918c2ecf20Sopenharmony_ci readbuf = buf; 928c2ecf20Sopenharmony_ci } else { 938c2ecf20Sopenharmony_ci readbuf = kmalloc(je32_to_cpu(ri->csize), GFP_KERNEL); 948c2ecf20Sopenharmony_ci if (!readbuf) { 958c2ecf20Sopenharmony_ci ret = -ENOMEM; 968c2ecf20Sopenharmony_ci goto out_ri; 978c2ecf20Sopenharmony_ci } 988c2ecf20Sopenharmony_ci } 998c2ecf20Sopenharmony_ci if (ri->compr != JFFS2_COMPR_NONE) { 1008c2ecf20Sopenharmony_ci if (len < je32_to_cpu(ri->dsize)) { 1018c2ecf20Sopenharmony_ci decomprbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL); 1028c2ecf20Sopenharmony_ci if (!decomprbuf) { 1038c2ecf20Sopenharmony_ci ret = -ENOMEM; 1048c2ecf20Sopenharmony_ci goto out_readbuf; 1058c2ecf20Sopenharmony_ci } 1068c2ecf20Sopenharmony_ci } else { 1078c2ecf20Sopenharmony_ci decomprbuf = buf; 1088c2ecf20Sopenharmony_ci } 1098c2ecf20Sopenharmony_ci } else { 1108c2ecf20Sopenharmony_ci decomprbuf = readbuf; 1118c2ecf20Sopenharmony_ci } 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci jffs2_dbg(2, "Read %d bytes to %p\n", je32_to_cpu(ri->csize), 1148c2ecf20Sopenharmony_ci readbuf); 1158c2ecf20Sopenharmony_ci ret = jffs2_flash_read(c, (ref_offset(fd->raw)) + sizeof(*ri), 1168c2ecf20Sopenharmony_ci je32_to_cpu(ri->csize), &readlen, readbuf); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci if (!ret && readlen != je32_to_cpu(ri->csize)) 1198c2ecf20Sopenharmony_ci ret = -EIO; 1208c2ecf20Sopenharmony_ci if (ret) 1218c2ecf20Sopenharmony_ci goto out_decomprbuf; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci crc = crc32(0, readbuf, je32_to_cpu(ri->csize)); 1248c2ecf20Sopenharmony_ci if (crc != je32_to_cpu(ri->data_crc)) { 1258c2ecf20Sopenharmony_ci pr_warn("Data CRC %08x != calculated CRC %08x for node at %08x\n", 1268c2ecf20Sopenharmony_ci je32_to_cpu(ri->data_crc), crc, ref_offset(fd->raw)); 1278c2ecf20Sopenharmony_ci ret = -EIO; 1288c2ecf20Sopenharmony_ci goto out_decomprbuf; 1298c2ecf20Sopenharmony_ci } 1308c2ecf20Sopenharmony_ci jffs2_dbg(2, "Data CRC matches calculated CRC %08x\n", crc); 1318c2ecf20Sopenharmony_ci if (ri->compr != JFFS2_COMPR_NONE) { 1328c2ecf20Sopenharmony_ci jffs2_dbg(2, "Decompress %d bytes from %p to %d bytes at %p\n", 1338c2ecf20Sopenharmony_ci je32_to_cpu(ri->csize), readbuf, 1348c2ecf20Sopenharmony_ci je32_to_cpu(ri->dsize), decomprbuf); 1358c2ecf20Sopenharmony_ci ret = jffs2_decompress(c, f, ri->compr | (ri->usercompr << 8), readbuf, decomprbuf, je32_to_cpu(ri->csize), je32_to_cpu(ri->dsize)); 1368c2ecf20Sopenharmony_ci if (ret) { 1378c2ecf20Sopenharmony_ci pr_warn("Error: jffs2_decompress returned %d\n", ret); 1388c2ecf20Sopenharmony_ci goto out_decomprbuf; 1398c2ecf20Sopenharmony_ci } 1408c2ecf20Sopenharmony_ci } 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci if (len < je32_to_cpu(ri->dsize)) { 1438c2ecf20Sopenharmony_ci memcpy(buf, decomprbuf+ofs, len); 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci out_decomprbuf: 1468c2ecf20Sopenharmony_ci if(decomprbuf != buf && decomprbuf != readbuf) 1478c2ecf20Sopenharmony_ci kfree(decomprbuf); 1488c2ecf20Sopenharmony_ci out_readbuf: 1498c2ecf20Sopenharmony_ci if(readbuf != buf) 1508c2ecf20Sopenharmony_ci kfree(readbuf); 1518c2ecf20Sopenharmony_ci out_ri: 1528c2ecf20Sopenharmony_ci jffs2_free_raw_inode(ri); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci return ret; 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ciint jffs2_read_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f, 1588c2ecf20Sopenharmony_ci unsigned char *buf, uint32_t offset, uint32_t len) 1598c2ecf20Sopenharmony_ci{ 1608c2ecf20Sopenharmony_ci uint32_t end = offset + len; 1618c2ecf20Sopenharmony_ci struct jffs2_node_frag *frag; 1628c2ecf20Sopenharmony_ci int ret; 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci jffs2_dbg(1, "%s(): ino #%u, range 0x%08x-0x%08x\n", 1658c2ecf20Sopenharmony_ci __func__, f->inocache->ino, offset, offset + len); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci frag = jffs2_lookup_node_frag(&f->fragtree, offset); 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci /* XXX FIXME: Where a single physical node actually shows up in two 1708c2ecf20Sopenharmony_ci frags, we read it twice. Don't do that. */ 1718c2ecf20Sopenharmony_ci /* Now we're pointing at the first frag which overlaps our page 1728c2ecf20Sopenharmony_ci * (or perhaps is before it, if we've been asked to read off the 1738c2ecf20Sopenharmony_ci * end of the file). */ 1748c2ecf20Sopenharmony_ci while(offset < end) { 1758c2ecf20Sopenharmony_ci jffs2_dbg(2, "%s(): offset %d, end %d\n", 1768c2ecf20Sopenharmony_ci __func__, offset, end); 1778c2ecf20Sopenharmony_ci if (unlikely(!frag || frag->ofs > offset || 1788c2ecf20Sopenharmony_ci frag->ofs + frag->size <= offset)) { 1798c2ecf20Sopenharmony_ci uint32_t holesize = end - offset; 1808c2ecf20Sopenharmony_ci if (frag && frag->ofs > offset) { 1818c2ecf20Sopenharmony_ci jffs2_dbg(1, "Eep. Hole in ino #%u fraglist. frag->ofs = 0x%08x, offset = 0x%08x\n", 1828c2ecf20Sopenharmony_ci f->inocache->ino, frag->ofs, offset); 1838c2ecf20Sopenharmony_ci holesize = min(holesize, frag->ofs - offset); 1848c2ecf20Sopenharmony_ci } 1858c2ecf20Sopenharmony_ci jffs2_dbg(1, "Filling non-frag hole from %d-%d\n", 1868c2ecf20Sopenharmony_ci offset, offset + holesize); 1878c2ecf20Sopenharmony_ci memset(buf, 0, holesize); 1888c2ecf20Sopenharmony_ci buf += holesize; 1898c2ecf20Sopenharmony_ci offset += holesize; 1908c2ecf20Sopenharmony_ci continue; 1918c2ecf20Sopenharmony_ci } else if (unlikely(!frag->node)) { 1928c2ecf20Sopenharmony_ci uint32_t holeend = min(end, frag->ofs + frag->size); 1938c2ecf20Sopenharmony_ci jffs2_dbg(1, "Filling frag hole from %d-%d (frag 0x%x 0x%x)\n", 1948c2ecf20Sopenharmony_ci offset, holeend, frag->ofs, 1958c2ecf20Sopenharmony_ci frag->ofs + frag->size); 1968c2ecf20Sopenharmony_ci memset(buf, 0, holeend - offset); 1978c2ecf20Sopenharmony_ci buf += holeend - offset; 1988c2ecf20Sopenharmony_ci offset = holeend; 1998c2ecf20Sopenharmony_ci frag = frag_next(frag); 2008c2ecf20Sopenharmony_ci continue; 2018c2ecf20Sopenharmony_ci } else { 2028c2ecf20Sopenharmony_ci uint32_t readlen; 2038c2ecf20Sopenharmony_ci uint32_t fragofs; /* offset within the frag to start reading */ 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci fragofs = offset - frag->ofs; 2068c2ecf20Sopenharmony_ci readlen = min(frag->size - fragofs, end - offset); 2078c2ecf20Sopenharmony_ci jffs2_dbg(1, "Reading %d-%d from node at 0x%08x (%d)\n", 2088c2ecf20Sopenharmony_ci frag->ofs+fragofs, 2098c2ecf20Sopenharmony_ci frag->ofs + fragofs+readlen, 2108c2ecf20Sopenharmony_ci ref_offset(frag->node->raw), 2118c2ecf20Sopenharmony_ci ref_flags(frag->node->raw)); 2128c2ecf20Sopenharmony_ci ret = jffs2_read_dnode(c, f, frag->node, buf, fragofs + frag->ofs - frag->node->ofs, readlen); 2138c2ecf20Sopenharmony_ci jffs2_dbg(2, "node read done\n"); 2148c2ecf20Sopenharmony_ci if (ret) { 2158c2ecf20Sopenharmony_ci jffs2_dbg(1, "%s(): error %d\n", 2168c2ecf20Sopenharmony_ci __func__, ret); 2178c2ecf20Sopenharmony_ci memset(buf, 0, readlen); 2188c2ecf20Sopenharmony_ci return ret; 2198c2ecf20Sopenharmony_ci } 2208c2ecf20Sopenharmony_ci buf += readlen; 2218c2ecf20Sopenharmony_ci offset += readlen; 2228c2ecf20Sopenharmony_ci frag = frag_next(frag); 2238c2ecf20Sopenharmony_ci jffs2_dbg(2, "node read was OK. Looping\n"); 2248c2ecf20Sopenharmony_ci } 2258c2ecf20Sopenharmony_ci } 2268c2ecf20Sopenharmony_ci return 0; 2278c2ecf20Sopenharmony_ci} 2288c2ecf20Sopenharmony_ci 229