[ar7] add support for Linux 3.6
[openwrt/svn-archive/archive.git] / target / linux / ar7 / files-3.6 / drivers / mtd / titanpart.c
1 #include <linux/kernel.h>
2 #include <linux/slab.h>
3
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>
9
10 #define IMAGE_A_SIZE 0X3c0000
11 #define WRTP_PARTS 14
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
17
18 static int part_count=0;
19 static struct mtd_partition titan_parts[WRTP_PARTS];
20
21
22 struct nsp_img_hdr_head
23 {
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
32 below */
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 */
37 unsigned int pad1;
38 };
39
40 struct nsp_img_hdr_section_info
41 {
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 */
45 };
46
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
50 {
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. */
58 };
59
60
61
62
63
64 static int titan_parse_env_address(char *env_name, unsigned int *flash_base,
65 unsigned int *flash_end)
66 {
67 char image_name[30];
68 char *env_ptr;
69 char *base_ptr;
70 char *end_ptr;
71 char * string_ptr;
72 /* Get the image variable */
73 env_ptr = prom_getenv(env_name);
74 if(!env_ptr){
75 printk("titan: invalid env name, %s.\n", env_name);
76 return -1; /* Error, no image variable */
77 }
78 strncpy(image_name, env_ptr, 30);
79 image_name[29]=0;
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);
86 return -1;
87 }
88
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);
94 return -1;
95 }
96 *flash_base &= 0x0fffffff;
97 *flash_end &= 0x0fffffff;
98 return 0;
99 }
100
101
102
103 static int titan_get_single_image(char *bootcfg_name, unsigned int *flash_base,
104 unsigned int *flash_end)
105 {
106 char *env_ptr;
107 char *base_ptr;
108 char *end_ptr;
109 char image_name[30];
110 char * string_ptr;
111
112 if(!bootcfg_name || !flash_base || !flash_end)
113 return -1;
114
115 env_ptr = prom_getenv(bootcfg_name);
116 if(!env_ptr){
117 printk("titan: %s variable not found.\n", bootcfg_name);
118 return -1; /* Error, no bootcfg variable */
119 }
120
121 string_ptr = image_name;
122 /* Save off the image name */
123 strncpy(image_name, env_ptr, 30);
124 image_name[29]=0;
125
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 */
131 }
132
133 /* Now, parse the addresses */
134 return titan_parse_env_address(base_ptr, flash_base, flash_end);
135 }
136
137
138
139 static void titan_add_partition(char * env_name, unsigned int flash_base, unsigned int flash_end)
140 {
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;
147 part_count++;
148
149 }
150 int create_titan_partitions(struct mtd_info *master,
151 struct mtd_partition **pparts,
152 unsigned long origin)
153 {
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;
159 char *name;
160 int i;
161 int total_sects=0;
162 size_t len;
163
164 /* Get the bootcfg env variable first */
165 if(titan_get_single_image("BOOTCFG", &flash_base, &flash_end)) {
166 /* Error, fallback */
167 return -1;
168 }
169
170 /* Get access to the header, and do some validation checks */
171 //hdr=(struct nsp_img_hdr_head*)flash_base;
172 mtd_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 */
175
176 mtd_read(master, flash_base + hdr.sect_info_offset, sizeof(struct nsp_img_hdr_section_info), &len, (uint8_t *)&sect_info);
177
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 mtd_read(master, flash_base + sect_info.sections_offset + (i * sect_info.sect_size) , sizeof(struct nsp_img_hdr_sections), &len, (uint8_t *)&section);
181 /* Add only the root partition */
182 if(section.type != NSP_IMG_SECTION_TYPE_FILESYSTEM_ROOT){
183 continue;
184 }
185 start=flash_base + section.offset;
186 end=start + section.total_size;
187 titan_add_partition("root", start, end);
188 total_sects++;
189
190 }
191
192 for(i=0; i< sect_info.num_sects && i<MAX_NUM_PARTITIONS; i++){
193
194 mtd_read(master, flash_base + sect_info.sections_offset + (i * sect_info.sect_size) , sizeof(struct nsp_img_hdr_sections), &len, (uint8_t *)&section);
195
196 name=section.name;
197 if(section.type == NSP_IMG_SECTION_TYPE_FILESYSTEM_ROOT)
198 {
199 name = "rootfs";
200 start=flash_base + section.offset;
201 end=flash_end;
202 titan_add_partition(name, start, end);
203 total_sects++;
204 }
205 else if(section.type == NSP_IMG_SECTION_TYPE_KERNEL)
206 {
207 name = "kernel";
208 start=flash_base + section.offset;
209 end=start + section.total_size;
210 titan_add_partition(name, start, end);
211 total_sects++;
212 }
213
214 }
215
216 /* Next, lets add the single image */
217 titan_add_partition("primary_image", flash_base, flash_end);
218 total_sects++;
219
220
221 titan_add_partition("full_image", 0, master->size);
222 total_sects++;
223
224 if (!titan_parse_env_address("BOOTLOADER", &start, &end)){
225 titan_add_partition("bootloader", start, end);
226 total_sects++;
227 }
228 if (!titan_parse_env_address("boot_env", &start, &end)){
229 titan_add_partition("boot_env", start, end);
230 total_sects++;
231 }
232 *pparts = titan_parts;
233 return total_sects;
234 }