1 #include <linux/kernel.h>
2 #include <linux/slab.h>
4 #include <linux/mtd/mtd.h>
5 #include <linux/mtd/partitions.h>
6 #include <linux/bootmem.h>
7 #include <linux/magic.h>
8 #include <asm/mach-ar7/prom.h>
10 #define IMAGE_A_SIZE 0X3c0000
12 #define NSP_IMG_MAGIC_NUMBER le32_to_cpu(0x4D544443)
13 #define NSP_IMG_SECTION_TYPE_KERNEL (0x01)
14 #define NSP_IMG_SECTION_TYPE_FILESYSTEM_ROOT (0x02)
15 #define NSP_IMG_SECTION_TYPE_FILESYSTEM (0x03)
16 #define MAX_NUM_PARTITIONS 14
18 static int part_count
=0;
19 static struct mtd_partition titan_parts
[WRTP_PARTS
];
22 struct nsp_img_hdr_head
24 unsigned int magic
; /* Magic number to identify this image header */
25 unsigned int boot_offset
; /* Offset from start of header to kernel code. */
26 unsigned int flags
; /* Image flags. */
27 unsigned int hdr_version
; /* Version of this header. */
28 unsigned int hdr_size
; /* The complete size of all portions of the header */
29 unsigned int prod_id
; /* This product id */
30 unsigned int rel_id
; /* Which release this is */
31 unsigned int version
; /* name-MMM.nnn.ooo-rxx => 0xMMnnooxx. See comment
33 unsigned int image_size
; /* Image size (including header) */
34 unsigned int info_offset
; /* Offset from start of header to info block */
35 unsigned int sect_info_offset
; /* Offset from start of header to section desc */
36 unsigned int chksum_offset
; /* Offset from start of header to chksum block */
40 struct nsp_img_hdr_section_info
42 unsigned int num_sects
; /* Number of section (and section desc blocks) in this image */
43 unsigned int sect_size
; /* Size of a SINGLE section_desc block */
44 unsigned int sections_offset
; /* Offset to from start of header to the start of the section blocks */
47 /* There will be one of more of the following stuctures in the image header. Each
48 section will have one of these blocks. */
49 struct nsp_img_hdr_sections
51 unsigned int offset
; /* Offset of section from start of NSP_IMG_HDR_HEAD */
52 unsigned int total_size
; /* Size of section (including pad size.) */
53 unsigned int raw_size
; /* Size of section only */
54 unsigned int flags
; /* Section flags */
55 unsigned int chksum
; /* Section checksum */
56 unsigned int type
; /* Section type. What kind of info does this section describe */
57 char name
[16]; /* Reference name for this section. */
64 static int titan_parse_env_address(char *env_name
, unsigned int *flash_base
,
65 unsigned int *flash_end
)
72 /* Get the image variable */
73 env_ptr
= prom_getenv(env_name
);
75 printk("titan: invalid env name, %s.\n", env_name
);
76 return -1; /* Error, no image variable */
78 strncpy(image_name
, env_ptr
, 30);
80 string_ptr
= image_name
;
81 /* Extract the start and stop addresses of the partition */
82 base_ptr
= strsep(&string_ptr
, ",");
83 end_ptr
= strsep(&string_ptr
, ",");
84 if ((base_ptr
== NULL
) || (end_ptr
== NULL
)) {
85 printk("titan: Couldn't tokenize %s start,end.\n", image_name
);
89 *flash_base
= (unsigned int) simple_strtol(base_ptr
, NULL
, 0);
90 *flash_end
= (unsigned int) simple_strtol(end_ptr
, NULL
, 0);
91 if((!*flash_base
) || (!*flash_end
)) {
92 printk("titan: Unable to convert :%s: :%s: into start,end values.\n",
93 env_name
, image_name
);
96 *flash_base
&= 0x0fffffff;
97 *flash_end
&= 0x0fffffff;
103 static int titan_get_single_image(char *bootcfg_name
, unsigned int *flash_base
,
104 unsigned int *flash_end
)
112 if(!bootcfg_name
|| !flash_base
|| !flash_end
)
115 env_ptr
= prom_getenv(bootcfg_name
);
117 printk("titan: %s variable not found.\n", bootcfg_name
);
118 return -1; /* Error, no bootcfg variable */
121 string_ptr
= image_name
;
122 /* Save off the image name */
123 strncpy(image_name
, env_ptr
, 30);
126 end_ptr
=strsep(&string_ptr
, "\"");
127 base_ptr
=strsep(&string_ptr
, "\""); /* Loose the last " */
128 if(!end_ptr
|| !base_ptr
){
129 printk("titan: invalid bootcfg format, %s.\n", image_name
);
130 return -1; /* Error, invalid bootcfg variable */
133 /* Now, parse the addresses */
134 return titan_parse_env_address(base_ptr
, flash_base
, flash_end
);
139 static void titan_add_partition(char * env_name
, unsigned int flash_base
, unsigned int flash_end
)
141 titan_parts
[part_count
].name
= env_name
;
142 titan_parts
[part_count
].offset
= flash_base
;
143 titan_parts
[part_count
].size
= flash_end
-flash_base
;
144 titan_parts
[part_count
].mask_flags
= (strcmp(env_name
, "bootloader")==0||
145 strcmp(env_name
, "boot_env")==0 ||
146 strcmp(env_name
, "full_image")==0 )?MTD_WRITEABLE
:0;
150 int create_titan_partitions(struct mtd_info
*master
,
151 struct mtd_partition
**pparts
,
152 unsigned long origin
)
154 struct nsp_img_hdr_head hdr
;
155 struct nsp_img_hdr_section_info sect_info
;
156 struct nsp_img_hdr_sections section
;
157 unsigned int flash_base
, flash_end
;
158 unsigned int start
, end
;
164 /* Get the bootcfg env variable first */
165 if(titan_get_single_image("BOOTCFG", &flash_base
, &flash_end
)) {
166 /* Error, fallback */
170 /* Get access to the header, and do some validation checks */
171 //hdr=(struct nsp_img_hdr_head*)flash_base;
172 master
->read(master
, flash_base
, sizeof(struct nsp_img_hdr_head
), &len
, (uint8_t *)&hdr
);
173 if(hdr
.magic
!= NSP_IMG_MAGIC_NUMBER
)
174 return -1; /* Not a single image */
176 master
->read(master
, flash_base
+ hdr
.sect_info_offset
, sizeof(struct nsp_img_hdr_section_info
), &len
, (uint8_t *)§_info
);
178 /* Look for the root fs, and add it first. This way we KNOW where the rootfs is */
179 for(i
=0; i
< sect_info
.num_sects
&& i
<MAX_NUM_PARTITIONS
; i
++){
180 master
->read(master
, flash_base
+ sect_info
.sections_offset
+ (i
* sect_info
.sect_size
) , sizeof(struct nsp_img_hdr_sections
), &len
, (uint8_t *)§ion
);
181 /* Add only the root partition */
182 if(section
.type
!= NSP_IMG_SECTION_TYPE_FILESYSTEM_ROOT
){
185 start
=flash_base
+ section
.offset
;
186 end
=start
+ section
.total_size
;
187 titan_add_partition("root", start
, end
);
192 for(i
=0; i
< sect_info
.num_sects
&& i
<MAX_NUM_PARTITIONS
; i
++){
194 master
->read(master
, flash_base
+ sect_info
.sections_offset
+ (i
* sect_info
.sect_size
) , sizeof(struct nsp_img_hdr_sections
), &len
, (uint8_t *)§ion
);
197 if(section
.type
== NSP_IMG_SECTION_TYPE_FILESYSTEM_ROOT
)
200 start
=flash_base
+ section
.offset
;
202 titan_add_partition(name
, start
, end
);
205 else if(section
.type
== NSP_IMG_SECTION_TYPE_KERNEL
)
208 start
=flash_base
+ section
.offset
;
209 end
=start
+ section
.total_size
;
210 titan_add_partition(name
, start
, end
);
216 /* Next, lets add the single image */
217 titan_add_partition("primary_image", flash_base
, flash_end
);
221 titan_add_partition("full_image", 0, master
->size
);
224 if (!titan_parse_env_address("BOOTLOADER", &start
, &end
)){
225 titan_add_partition("bootloader", start
, end
);
228 if (!titan_parse_env_address("boot_env", &start
, &end
)){
229 titan_add_partition("boot_env", start
, end
);
232 *pparts
= titan_parts
;