OpenWrt – Blame information for rev 4
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
4 | office | 1 | /* |
2 | * Copyright (C) 2009 Christian Daniel <cd@maintech.de> |
||
3 | * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org> |
||
4 | * |
||
5 | * This program is free software; you can redistribute it and/or modify |
||
6 | * it under the terms of the GNU General Public License as published by |
||
7 | * the Free Software Foundation; either version 2 of the License, or |
||
8 | * (at your option) any later version. |
||
9 | * |
||
10 | * This program is distributed in the hope that it will be useful, |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | * GNU General Public License for more details. |
||
14 | * |
||
15 | * You should have received a copy of the GNU General Public License |
||
16 | * along with this program; if not, write to the Free Software |
||
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
||
18 | * |
||
19 | * TRX flash partition table. |
||
20 | * Based on ar7 map by Felix Fietkau <nbd@nbd.name> |
||
21 | * |
||
22 | */ |
||
23 | |||
24 | #include <linux/kernel.h> |
||
25 | #include <linux/module.h> |
||
26 | #include <linux/slab.h> |
||
27 | #include <linux/vmalloc.h> |
||
28 | |||
29 | #include <linux/mtd/mtd.h> |
||
30 | #include <linux/mtd/partitions.h> |
||
31 | #include <linux/version.h> |
||
32 | |||
33 | struct cybertan_header { |
||
34 | char magic[4]; |
||
35 | u8 res1[4]; |
||
36 | char fw_date[3]; |
||
37 | char fw_ver[3]; |
||
38 | char id[4]; |
||
39 | char hw_ver; |
||
40 | char unused; |
||
41 | u8 flags[2]; |
||
42 | u8 res2[10]; |
||
43 | }; |
||
44 | |||
45 | #define TRX_PARTS 6 |
||
46 | #define TRX_MAGIC 0x30524448 |
||
47 | #define TRX_MAX_OFFSET 3 |
||
48 | |||
49 | struct trx_header { |
||
50 | uint32_t magic; /* "HDR0" */ |
||
51 | uint32_t len; /* Length of file including header */ |
||
52 | uint32_t crc32; /* 32-bit CRC from flag_version to end of file */ |
||
53 | uint32_t flag_version; /* 0:15 flags, 16:31 version */ |
||
54 | uint32_t offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */ |
||
55 | }; |
||
56 | |||
57 | #define IH_MAGIC 0x27051956 /* Image Magic Number */ |
||
58 | #define IH_NMLEN 32 /* Image Name Length */ |
||
59 | |||
60 | struct uimage_header { |
||
61 | uint32_t ih_magic; /* Image Header Magic Number */ |
||
62 | uint32_t ih_hcrc; /* Image Header CRC Checksum */ |
||
63 | uint32_t ih_time; /* Image Creation Timestamp */ |
||
64 | uint32_t ih_size; /* Image Data Size */ |
||
65 | uint32_t ih_load; /* Data» Load Address */ |
||
66 | uint32_t ih_ep; /* Entry Point Address */ |
||
67 | uint32_t ih_dcrc; /* Image Data CRC Checksum */ |
||
68 | uint8_t ih_os; /* Operating System */ |
||
69 | uint8_t ih_arch; /* CPU architecture */ |
||
70 | uint8_t ih_type; /* Image Type */ |
||
71 | uint8_t ih_comp; /* Compression Type */ |
||
72 | uint8_t ih_name[IH_NMLEN]; /* Image Name */ |
||
73 | }; |
||
74 | |||
75 | struct firmware_header { |
||
76 | struct cybertan_header cybertan; |
||
77 | struct trx_header trx; |
||
78 | struct uimage_header uimage; |
||
79 | } __packed; |
||
80 | |||
81 | #define UBOOT_LEN 0x40000 |
||
82 | #define ART_LEN 0x10000 |
||
83 | #define NVRAM_LEN 0x10000 |
||
84 | |||
85 | static int cybertan_parse_partitions(struct mtd_info *master, |
||
86 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0) |
||
87 | struct mtd_partition **pparts, |
||
88 | #else |
||
89 | const struct mtd_partition **pparts, |
||
90 | #endif |
||
91 | struct mtd_part_parser_data *data) |
||
92 | { |
||
93 | struct firmware_header *header; |
||
94 | struct trx_header *theader; |
||
95 | struct uimage_header *uheader; |
||
96 | struct mtd_partition *trx_parts; |
||
97 | size_t retlen; |
||
98 | unsigned int kernel_len; |
||
99 | unsigned int uboot_len; |
||
100 | unsigned int nvram_len; |
||
101 | unsigned int art_len; |
||
102 | int ret; |
||
103 | |||
104 | uboot_len = max_t(unsigned int, master->erasesize, UBOOT_LEN); |
||
105 | nvram_len = max_t(unsigned int, master->erasesize, NVRAM_LEN); |
||
106 | art_len = max_t(unsigned int, master->erasesize, ART_LEN); |
||
107 | |||
108 | trx_parts = kzalloc(TRX_PARTS * sizeof(struct mtd_partition), |
||
109 | GFP_KERNEL); |
||
110 | if (!trx_parts) { |
||
111 | ret = -ENOMEM; |
||
112 | goto out; |
||
113 | } |
||
114 | |||
115 | header = vmalloc(sizeof(*header)); |
||
116 | if (!header) { |
||
117 | return -ENOMEM; |
||
118 | goto free_parts; |
||
119 | } |
||
120 | |||
121 | ret = mtd_read(master, uboot_len, sizeof(*header), |
||
122 | &retlen, (void *) header); |
||
123 | if (ret) |
||
124 | goto free_hdr; |
||
125 | |||
126 | if (retlen != sizeof(*header)) { |
||
127 | ret = -EIO; |
||
128 | goto free_hdr; |
||
129 | } |
||
130 | |||
131 | theader = &header->trx; |
||
132 | if (le32_to_cpu(theader->magic) != TRX_MAGIC) { |
||
133 | printk(KERN_NOTICE "%s: no TRX header found\n", master->name); |
||
134 | goto free_hdr; |
||
135 | } |
||
136 | |||
137 | uheader = &header->uimage; |
||
138 | if (uheader->ih_magic != IH_MAGIC) { |
||
139 | printk(KERN_NOTICE "%s: no uImage found\n", master->name); |
||
140 | goto free_hdr; |
||
141 | } |
||
142 | |||
143 | kernel_len = le32_to_cpu(theader->offsets[1]) + |
||
144 | sizeof(struct cybertan_header); |
||
145 | |||
146 | trx_parts[0].name = "u-boot"; |
||
147 | trx_parts[0].offset = 0; |
||
148 | trx_parts[0].size = uboot_len; |
||
149 | trx_parts[0].mask_flags = MTD_WRITEABLE; |
||
150 | |||
151 | trx_parts[1].name = "kernel"; |
||
152 | trx_parts[1].offset = trx_parts[0].offset + trx_parts[0].size; |
||
153 | trx_parts[1].size = kernel_len; |
||
154 | trx_parts[1].mask_flags = 0; |
||
155 | |||
156 | trx_parts[2].name = "rootfs"; |
||
157 | trx_parts[2].offset = trx_parts[1].offset + trx_parts[1].size; |
||
158 | trx_parts[2].size = master->size - uboot_len - nvram_len - art_len - |
||
159 | trx_parts[1].size; |
||
160 | trx_parts[2].mask_flags = 0; |
||
161 | |||
162 | trx_parts[3].name = "nvram"; |
||
163 | trx_parts[3].offset = master->size - nvram_len - art_len; |
||
164 | trx_parts[3].size = nvram_len; |
||
165 | trx_parts[3].mask_flags = MTD_WRITEABLE; |
||
166 | |||
167 | trx_parts[4].name = "art"; |
||
168 | trx_parts[4].offset = master->size - art_len; |
||
169 | trx_parts[4].size = art_len; |
||
170 | trx_parts[4].mask_flags = MTD_WRITEABLE; |
||
171 | |||
172 | trx_parts[5].name = "firmware"; |
||
173 | trx_parts[5].offset = uboot_len; |
||
174 | trx_parts[5].size = master->size - uboot_len - nvram_len - art_len; |
||
175 | trx_parts[5].mask_flags = 0; |
||
176 | |||
177 | vfree(header); |
||
178 | |||
179 | *pparts = trx_parts; |
||
180 | return TRX_PARTS; |
||
181 | |||
182 | free_hdr: |
||
183 | vfree(header); |
||
184 | free_parts: |
||
185 | kfree(trx_parts); |
||
186 | out: |
||
187 | return ret; |
||
188 | } |
||
189 | |||
190 | static struct mtd_part_parser cybertan_parser = { |
||
191 | .owner = THIS_MODULE, |
||
192 | .parse_fn = cybertan_parse_partitions, |
||
193 | .name = "cybertan", |
||
194 | }; |
||
195 | |||
196 | static int __init cybertan_parser_init(void) |
||
197 | { |
||
198 | register_mtd_parser(&cybertan_parser); |
||
199 | |||
200 | return 0; |
||
201 | } |
||
202 | |||
203 | module_init(cybertan_parser_init); |
||
204 | |||
205 | MODULE_LICENSE("GPL"); |
||
206 | MODULE_AUTHOR("Christian Daniel <cd@maintech.de>"); |