ath79: port cybertan_part from ar71xx
[openwrt/openwrt.git] / target / linux / ath79 / files / drivers / mtd / parsers / parser_cybertan.c
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 } __packed;
44
45 #define TRX_PARTS 3
46 #define TRX_MAGIC 0x30524448
47 #define TRX_MAX_OFFSET 3
48
49 struct trx_header {
50 __le32 magic; /* "HDR0" */
51 __le32 len; /* Length of file including header */
52 __le32 crc32; /* 32-bit CRC from flag_version to end of file */
53 __le32 flag_version; /* 0:15 flags, 16:31 version */
54 __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
55 } __packed;
56
57 #define IH_MAGIC 0x27051956 /* Image Magic Number */
58 #define IH_NMLEN 32 /* Image Name Length */
59
60 struct uimage_header {
61 __be32 ih_magic; /* Image Header Magic Number */
62 __be32 ih_hcrc; /* Image Header CRC Checksum */
63 __be32 ih_time; /* Image Creation Timestamp */
64 __be32 ih_size; /* Image Data Size */
65 __be32 ih_load; /* Data» Load Address */
66 __be32 ih_ep; /* Entry Point Address */
67 __be32 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 } __packed;
74
75 struct firmware_header {
76 struct cybertan_header cybertan;
77 struct trx_header trx;
78 struct uimage_header uimage;
79 } __packed;
80
81 static int cybertan_parse_partitions(struct mtd_info *master,
82 const struct mtd_partition **pparts,
83 struct mtd_part_parser_data *data)
84 {
85 struct firmware_header header;
86 struct trx_header *theader;
87 struct uimage_header *uheader;
88 struct mtd_partition *trx_parts;
89 size_t retlen;
90 unsigned int kernel_len;
91 int ret;
92
93 trx_parts = kcalloc(TRX_PARTS, sizeof(struct mtd_partition),
94 GFP_KERNEL);
95 if (!trx_parts) {
96 ret = -ENOMEM;
97 goto out;
98 }
99
100 ret = mtd_read(master, 0, sizeof(header),
101 &retlen, (uint8_t *)&header);
102 if (ret)
103 goto free_parts;
104
105 if (retlen != sizeof(header)) {
106 ret = -EIO;
107 goto free_parts;
108 }
109
110 theader = &header.trx;
111 if (theader->magic != cpu_to_le32(TRX_MAGIC)) {
112 printk(KERN_NOTICE "%s: no TRX header found\n", master->name);
113 goto free_parts;
114 }
115
116 uheader = &header.uimage;
117 if (uheader->ih_magic != cpu_to_be32(IH_MAGIC)) {
118 printk(KERN_NOTICE "%s: no uImage found\n", master->name);
119 goto free_parts;
120 }
121
122 kernel_len = le32_to_cpu(theader->offsets[1]) +
123 sizeof(struct cybertan_header);
124
125 trx_parts[0].name = "header";
126 trx_parts[0].offset = 0;
127 trx_parts[0].size = offsetof(struct firmware_header, uimage);
128 trx_parts[0].mask_flags = 0;
129
130 trx_parts[1].name = "kernel";
131 trx_parts[1].offset = trx_parts[0].offset + trx_parts[0].size;
132 trx_parts[1].size = kernel_len - trx_parts[0].size;
133 trx_parts[1].mask_flags = 0;
134
135 trx_parts[2].name = "rootfs";
136 trx_parts[2].offset = trx_parts[1].offset + trx_parts[1].size;
137 trx_parts[2].size = master->size - trx_parts[1].size - trx_parts[0].size;
138 trx_parts[2].mask_flags = 0;
139
140 *pparts = trx_parts;
141 return TRX_PARTS;
142
143 free_parts:
144 kfree(trx_parts);
145 out:
146 return ret;
147 }
148
149 static const struct of_device_id mtd_parser_cybertan_of_match_table[] = {
150 { .compatible = "cybertan,trx" },
151 {},
152 };
153 MODULE_DEVICE_TABLE(of, mtd_parser_cybertan_of_match_table);
154
155 static struct mtd_part_parser mtd_parser_cybertan = {
156 .parse_fn = cybertan_parse_partitions,
157 .name = "cybertan-trx",
158 .of_match_table = mtd_parser_cybertan_of_match_table,
159 };
160 module_mtd_part_parser(mtd_parser_cybertan);
161
162 MODULE_LICENSE("GPL");
163 MODULE_AUTHOR("Christian Daniel <cd@maintech.de>");