2 * Copyright © 2009 Christian Daniel <cd@maintech.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * TRX flash partition table.
19 * Based on ar7 map by Felix Fietkau <nbd@openwrt.org>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/partitions.h>
28 #include <linux/bootmem.h>
29 #include <linux/magic.h>
32 #define TRX_MAGIC 0x30524448
33 #define TRX_MAX_OFFSET 3
36 uint32_t magic; /* "HDR0" */
37 uint32_t len; /* Length of file including header */
38 uint32_t crc32; /* 32-bit CRC from flag_version to end of file */
39 uint32_t flag_version; /* 0:15 flags, 16:31 version */
40 uint32_t offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
43 #define IH_MAGIC 0x27051956 /* Image Magic Number */
44 #define IH_NMLEN 32 /* Image Name Length */
46 struct uimage_header {
47 uint32_t ih_magic; /* Image Header Magic Number */
48 uint32_t ih_hcrc; /* Image Header CRC Checksum */
49 uint32_t ih_time; /* Image Creation Timestamp */
50 uint32_t ih_size; /* Image Data Size */
51 uint32_t ih_load; /* Data» Load Address */
52 uint32_t ih_ep; /* Entry Point Address */
53 uint32_t ih_dcrc; /* Image Data CRC Checksum */
54 uint8_t ih_os; /* Operating System */
55 uint8_t ih_arch; /* CPU architecture */
56 uint8_t ih_type; /* Image Type */
57 uint8_t ih_comp; /* Compression Type */
58 uint8_t ih_name[IH_NMLEN]; /* Image Name */
61 static struct mtd_partition trx_parts[TRX_PARTS];
63 static int create_mtd_partitions(struct mtd_info *master,
64 struct mtd_partition **pparts,
69 struct trx_header *header;
70 struct uimage_header *uheader;
71 unsigned int kernel_len;
73 master->read(master, 4 * master->erasesize, sizeof(buf), &len, buf);
74 if (strncmp(buf, "NL16", 4) == 0) {
75 printk(KERN_INFO "TRX on WRT160NL detected\n");
77 header = (struct trx_header *)(buf + 32);
78 if (le32_to_cpu(header->magic) != TRX_MAGIC) {
79 printk(KERN_WARNING "TRX messed up\n");
83 uheader = (struct uimage_header *)(buf + 60);
84 if (uheader->ih_magic != IH_MAGIC) {
85 printk(KERN_WARNING "uImage messed up\n");
89 kernel_len = uheader->ih_size / master->erasesize;
90 if (uheader->ih_size % master->erasesize)
94 kernel_len *= master->erasesize;
96 trx_parts[0].name = "u-boot";
97 trx_parts[0].offset = 0;
98 trx_parts[0].size = 4 * master->erasesize;
99 trx_parts[0].mask_flags = MTD_WRITEABLE;
101 trx_parts[1].name = "kernel";
102 trx_parts[1].offset = trx_parts[0].offset + trx_parts[0].size;
103 trx_parts[1].size = kernel_len;
104 trx_parts[1].mask_flags = 0;
106 trx_parts[2].name = "rootfs";
107 trx_parts[2].offset = trx_parts[1].offset + trx_parts[1].size;
108 trx_parts[2].size = master->size - 6 * master->erasesize - trx_parts[1].size;
109 trx_parts[2].mask_flags = 0;
111 trx_parts[3].name = "nvram";
112 trx_parts[3].offset = master->size - 2 * master->erasesize;
113 trx_parts[3].size = master->erasesize;
114 trx_parts[3].mask_flags = MTD_WRITEABLE;
116 trx_parts[4].name = "art";
117 trx_parts[4].offset = master->size - master->erasesize;
118 trx_parts[4].size = master->erasesize;
119 trx_parts[4].mask_flags = MTD_WRITEABLE;
121 trx_parts[5].name = "firmware";
122 trx_parts[5].offset = 4 * master->erasesize;
123 trx_parts[5].size = master->size - 6 * master->erasesize;
124 trx_parts[5].mask_flags = 0;
134 static struct mtd_part_parser trx_parser = {
135 .owner = THIS_MODULE,
136 .parse_fn = create_mtd_partitions,
140 static int __init trx_parser_init(void)
142 return register_mtd_parser(&trx_parser);
145 module_init(trx_parser_init);
147 MODULE_LICENSE("GPL");
148 MODULE_AUTHOR("Christian Daniel <cd@maintech.de>");