2 * Copyright (C) 2009 Christian Daniel <cd@maintech.de>
3 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
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.
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.
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
19 * TRX flash partition table.
20 * Based on ar7 map by Felix Fietkau <nbd@nbd.name>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/partitions.h>
31 #include <linux/version.h>
33 struct cybertan_header
{
46 #define TRX_MAGIC 0x30524448
47 #define TRX_MAX_OFFSET 3
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 */
57 #define IH_MAGIC 0x27051956 /* Image Magic Number */
58 #define IH_NMLEN 32 /* Image Name Length */
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 */
75 struct firmware_header
{
76 struct cybertan_header cybertan
;
77 struct trx_header trx
;
78 struct uimage_header uimage
;
81 static int cybertan_parse_partitions(struct mtd_info
*master
,
82 const struct mtd_partition
**pparts
,
83 struct mtd_part_parser_data
*data
)
85 struct firmware_header header
;
86 struct trx_header
*theader
;
87 struct uimage_header
*uheader
;
88 struct mtd_partition
*trx_parts
;
90 unsigned int kernel_len
;
93 trx_parts
= kcalloc(TRX_PARTS
, sizeof(struct mtd_partition
),
100 ret
= mtd_read(master
, 0, sizeof(header
),
101 &retlen
, (uint8_t *)&header
);
105 if (retlen
!= sizeof(header
)) {
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
);
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
);
122 kernel_len
= le32_to_cpu(theader
->offsets
[1]) +
123 sizeof(struct cybertan_header
);
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;
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;
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;
149 static const struct of_device_id mtd_parser_cybertan_of_match_table
[] = {
150 { .compatible
= "cybertan,trx" },
153 MODULE_DEVICE_TABLE(of
, mtd_parser_cybertan_of_match_table
);
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
,
160 module_mtd_part_parser(mtd_parser_cybertan
);
162 MODULE_LICENSE("GPL");
163 MODULE_AUTHOR("Christian Daniel <cd@maintech.de>");