OpenWrt – Blame information for rev 4
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
4 | office | 1 | /* |
2 | * Copyright (C) 2009-2013 Felix Fietkau <nbd@nbd.name> |
||
3 | * Copyright (C) 2009-2013 Gabor Juhos <juhosg@openwrt.org> |
||
4 | * Copyright (C) 2012 Jonas Gorski <jogo@openwrt.org> |
||
5 | * Copyright (C) 2013 Hauke Mehrtens <hauke@hauke-m.de> |
||
6 | * |
||
7 | * This program is free software; you can redistribute it and/or modify it |
||
8 | * under the terms of the GNU General Public License version 2 as published |
||
9 | * by the Free Software Foundation. |
||
10 | * |
||
11 | */ |
||
12 | |||
13 | #define pr_fmt(fmt) "mtdsplit: " fmt |
||
14 | |||
15 | #include <linux/export.h> |
||
16 | #include <linux/init.h> |
||
17 | #include <linux/kernel.h> |
||
18 | #include <linux/magic.h> |
||
19 | #include <linux/mtd/mtd.h> |
||
20 | #include <linux/mtd/partitions.h> |
||
21 | #include <linux/byteorder/generic.h> |
||
22 | |||
23 | #include "mtdsplit.h" |
||
24 | |||
25 | #define UBI_EC_MAGIC 0x55424923 /* UBI# */ |
||
26 | |||
27 | struct squashfs_super_block { |
||
28 | __le32 s_magic; |
||
29 | __le32 pad0[9]; |
||
30 | __le64 bytes_used; |
||
31 | }; |
||
32 | |||
33 | int mtd_get_squashfs_len(struct mtd_info *master, |
||
34 | size_t offset, |
||
35 | size_t *squashfs_len) |
||
36 | { |
||
37 | struct squashfs_super_block sb; |
||
38 | size_t retlen; |
||
39 | int err; |
||
40 | |||
41 | err = mtd_read(master, offset, sizeof(sb), &retlen, (void *)&sb); |
||
42 | if (err || (retlen != sizeof(sb))) { |
||
43 | pr_alert("error occured while reading from \"%s\"\n", |
||
44 | master->name); |
||
45 | return -EIO; |
||
46 | } |
||
47 | |||
48 | if (le32_to_cpu(sb.s_magic) != SQUASHFS_MAGIC) { |
||
49 | pr_alert("no squashfs found in \"%s\"\n", master->name); |
||
50 | return -EINVAL; |
||
51 | } |
||
52 | |||
53 | retlen = le64_to_cpu(sb.bytes_used); |
||
54 | if (retlen <= 0) { |
||
55 | pr_alert("squashfs is empty in \"%s\"\n", master->name); |
||
56 | return -ENODEV; |
||
57 | } |
||
58 | |||
59 | if (offset + retlen > master->size) { |
||
60 | pr_alert("squashfs has invalid size in \"%s\"\n", |
||
61 | master->name); |
||
62 | return -EINVAL; |
||
63 | } |
||
64 | |||
65 | *squashfs_len = retlen; |
||
66 | return 0; |
||
67 | } |
||
68 | EXPORT_SYMBOL_GPL(mtd_get_squashfs_len); |
||
69 | |||
70 | static ssize_t mtd_next_eb(struct mtd_info *mtd, size_t offset) |
||
71 | { |
||
72 | return mtd_rounddown_to_eb(offset, mtd) + mtd->erasesize; |
||
73 | } |
||
74 | |||
75 | int mtd_check_rootfs_magic(struct mtd_info *mtd, size_t offset, |
||
76 | enum mtdsplit_part_type *type) |
||
77 | { |
||
78 | u32 magic; |
||
79 | size_t retlen; |
||
80 | int ret; |
||
81 | |||
82 | ret = mtd_read(mtd, offset, sizeof(magic), &retlen, |
||
83 | (unsigned char *) &magic); |
||
84 | if (ret) |
||
85 | return ret; |
||
86 | |||
87 | if (retlen != sizeof(magic)) |
||
88 | return -EIO; |
||
89 | |||
90 | if (le32_to_cpu(magic) == SQUASHFS_MAGIC) { |
||
91 | if (type) |
||
92 | *type = MTDSPLIT_PART_TYPE_SQUASHFS; |
||
93 | return 0; |
||
94 | } else if (magic == 0x19852003) { |
||
95 | if (type) |
||
96 | *type = MTDSPLIT_PART_TYPE_JFFS2; |
||
97 | return 0; |
||
98 | } else if (be32_to_cpu(magic) == UBI_EC_MAGIC) { |
||
99 | if (type) |
||
100 | *type = MTDSPLIT_PART_TYPE_UBI; |
||
101 | return 0; |
||
102 | } |
||
103 | |||
104 | return -EINVAL; |
||
105 | } |
||
106 | EXPORT_SYMBOL_GPL(mtd_check_rootfs_magic); |
||
107 | |||
108 | int mtd_find_rootfs_from(struct mtd_info *mtd, |
||
109 | size_t from, |
||
110 | size_t limit, |
||
111 | size_t *ret_offset, |
||
112 | enum mtdsplit_part_type *type) |
||
113 | { |
||
114 | size_t offset; |
||
115 | int err; |
||
116 | |||
117 | for (offset = from; offset < limit; |
||
118 | offset = mtd_next_eb(mtd, offset)) { |
||
119 | err = mtd_check_rootfs_magic(mtd, offset, type); |
||
120 | if (err) |
||
121 | continue; |
||
122 | |||
123 | *ret_offset = offset; |
||
124 | return 0; |
||
125 | } |
||
126 | |||
127 | return -ENODEV; |
||
128 | } |
||
129 | EXPORT_SYMBOL_GPL(mtd_find_rootfs_from); |
||
130 |