titanpart.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <linux/kernel.h>
  2. #include <linux/slab.h>
  3. #include <linux/mtd/mtd.h>
  4. #include <linux/mtd/partitions.h>
  5. #include <linux/bootmem.h>
  6. #include <linux/magic.h>
  7. #include <asm/mach-ar7/prom.h>
  8. #define IMAGE_A_SIZE 0X3c0000
  9. #define WRTP_PARTS 14
  10. #define NSP_IMG_MAGIC_NUMBER le32_to_cpu(0x4D544443)
  11. #define NSP_IMG_SECTION_TYPE_KERNEL (0x01)
  12. #define NSP_IMG_SECTION_TYPE_FILESYSTEM_ROOT (0x02)
  13. #define NSP_IMG_SECTION_TYPE_FILESYSTEM (0x03)
  14. #define MAX_NUM_PARTITIONS 14
  15. static int part_count=0;
  16. static struct mtd_partition titan_parts[WRTP_PARTS];
  17. struct nsp_img_hdr_head
  18. {
  19. unsigned int magic; /* Magic number to identify this image header */
  20. unsigned int boot_offset; /* Offset from start of header to kernel code. */
  21. unsigned int flags; /* Image flags. */
  22. unsigned int hdr_version; /* Version of this header. */
  23. unsigned int hdr_size; /* The complete size of all portions of the header */
  24. unsigned int prod_id; /* This product id */
  25. unsigned int rel_id; /* Which release this is */
  26. unsigned int version; /* name-MMM.nnn.ooo-rxx => 0xMMnnooxx. See comment
  27. below */
  28. unsigned int image_size; /* Image size (including header) */
  29. unsigned int info_offset; /* Offset from start of header to info block */
  30. unsigned int sect_info_offset; /* Offset from start of header to section desc */
  31. unsigned int chksum_offset; /* Offset from start of header to chksum block */
  32. unsigned int pad1;
  33. };
  34. struct nsp_img_hdr_section_info
  35. {
  36. unsigned int num_sects; /* Number of section (and section desc blocks) in this image */
  37. unsigned int sect_size; /* Size of a SINGLE section_desc block */
  38. unsigned int sections_offset; /* Offset to from start of header to the start of the section blocks */
  39. };
  40. /* There will be one of more of the following stuctures in the image header. Each
  41. section will have one of these blocks. */
  42. struct nsp_img_hdr_sections
  43. {
  44. unsigned int offset; /* Offset of section from start of NSP_IMG_HDR_HEAD */
  45. unsigned int total_size; /* Size of section (including pad size.) */
  46. unsigned int raw_size; /* Size of section only */
  47. unsigned int flags; /* Section flags */
  48. unsigned int chksum; /* Section checksum */
  49. unsigned int type; /* Section type. What kind of info does this section describe */
  50. char name[16]; /* Reference name for this section. */
  51. };
  52. static int titan_parse_env_address(char *env_name, unsigned int *flash_base,
  53. unsigned int *flash_end)
  54. {
  55. char image_name[30];
  56. char *env_ptr;
  57. char *base_ptr;
  58. char *end_ptr;
  59. char * string_ptr;
  60. /* Get the image variable */
  61. env_ptr = prom_getenv(env_name);
  62. if(!env_ptr){
  63. printk("titan: invalid env name, %s.\n", env_name);
  64. return -1; /* Error, no image variable */
  65. }
  66. strncpy(image_name, env_ptr, 30);
  67. image_name[29]=0;
  68. string_ptr = image_name;
  69. /* Extract the start and stop addresses of the partition */
  70. base_ptr = strsep(&string_ptr, ",");
  71. end_ptr = strsep(&string_ptr, ",");
  72. if ((base_ptr == NULL) || (end_ptr == NULL)) {
  73. printk("titan: Couldn't tokenize %s start,end.\n", image_name);
  74. return -1;
  75. }
  76. *flash_base = (unsigned int) simple_strtol(base_ptr, NULL, 0);
  77. *flash_end = (unsigned int) simple_strtol(end_ptr, NULL, 0);
  78. if((!*flash_base) || (!*flash_end)) {
  79. printk("titan: Unable to convert :%s: :%s: into start,end values.\n",
  80. env_name, image_name);
  81. return -1;
  82. }
  83. *flash_base &= 0x0fffffff;
  84. *flash_end &= 0x0fffffff;
  85. return 0;
  86. }
  87. static int titan_get_single_image(char *bootcfg_name, unsigned int *flash_base,
  88. unsigned int *flash_end)
  89. {
  90. char *env_ptr;
  91. char *base_ptr;
  92. char *end_ptr;
  93. char image_name[30];
  94. char * string_ptr;
  95. if(!bootcfg_name || !flash_base || !flash_end)
  96. return -1;
  97. env_ptr = prom_getenv(bootcfg_name);
  98. if(!env_ptr){
  99. printk("titan: %s variable not found.\n", bootcfg_name);
  100. return -1; /* Error, no bootcfg variable */
  101. }
  102. string_ptr = image_name;
  103. /* Save off the image name */
  104. strncpy(image_name, env_ptr, 30);
  105. image_name[29]=0;
  106. end_ptr=strsep(&string_ptr, "\"");
  107. base_ptr=strsep(&string_ptr, "\""); /* Loose the last " */
  108. if(!end_ptr || !base_ptr){
  109. printk("titan: invalid bootcfg format, %s.\n", image_name);
  110. return -1; /* Error, invalid bootcfg variable */
  111. }
  112. /* Now, parse the addresses */
  113. return titan_parse_env_address(base_ptr, flash_base, flash_end);
  114. }
  115. static void titan_add_partition(char * env_name, unsigned int flash_base, unsigned int flash_end)
  116. {
  117. titan_parts[part_count].name = env_name;
  118. titan_parts[part_count].offset = flash_base;
  119. titan_parts[part_count].size = flash_end-flash_base;
  120. titan_parts[part_count].mask_flags = (strcmp(env_name, "bootloader")==0||
  121. strcmp(env_name, "boot_env")==0 ||
  122. strcmp(env_name, "full_image")==0 )?MTD_WRITEABLE:0;
  123. part_count++;
  124. }
  125. int create_titan_partitions(struct mtd_info *master,
  126. struct mtd_partition **pparts,
  127. unsigned long origin)
  128. {
  129. struct nsp_img_hdr_head hdr;
  130. struct nsp_img_hdr_section_info sect_info;
  131. struct nsp_img_hdr_sections section;
  132. unsigned int flash_base, flash_end;
  133. unsigned int start, end;
  134. char *name;
  135. int i;
  136. int total_sects=0;
  137. size_t len;
  138. /* Get the bootcfg env variable first */
  139. if(titan_get_single_image("BOOTCFG", &flash_base, &flash_end)) {
  140. /* Error, fallback */
  141. return -1;
  142. }
  143. /* Get access to the header, and do some validation checks */
  144. //hdr=(struct nsp_img_hdr_head*)flash_base;
  145. mtd_read(master, flash_base, sizeof(struct nsp_img_hdr_head), &len, (uint8_t *)&hdr);
  146. if(hdr.magic != NSP_IMG_MAGIC_NUMBER)
  147. return -1; /* Not a single image */
  148. mtd_read(master, flash_base + hdr.sect_info_offset, sizeof(struct nsp_img_hdr_section_info), &len, (uint8_t *)&sect_info);
  149. /* Look for the root fs, and add it first. This way we KNOW where the rootfs is */
  150. for(i=0; i< sect_info.num_sects && i<MAX_NUM_PARTITIONS; i++){
  151. mtd_read(master, flash_base + sect_info.sections_offset + (i * sect_info.sect_size) , sizeof(struct nsp_img_hdr_sections), &len, (uint8_t *)&section);
  152. /* Add only the root partition */
  153. if(section.type != NSP_IMG_SECTION_TYPE_FILESYSTEM_ROOT){
  154. continue;
  155. }
  156. start=flash_base + section.offset;
  157. end=start + section.total_size;
  158. titan_add_partition("root", start, end);
  159. total_sects++;
  160. }
  161. for(i=0; i< sect_info.num_sects && i<MAX_NUM_PARTITIONS; i++){
  162. mtd_read(master, flash_base + sect_info.sections_offset + (i * sect_info.sect_size) , sizeof(struct nsp_img_hdr_sections), &len, (uint8_t *)&section);
  163. name=section.name;
  164. if(section.type == NSP_IMG_SECTION_TYPE_FILESYSTEM_ROOT)
  165. {
  166. name = "rootfs";
  167. start=flash_base + section.offset;
  168. end=flash_end;
  169. titan_add_partition(name, start, end);
  170. total_sects++;
  171. }
  172. else if(section.type == NSP_IMG_SECTION_TYPE_KERNEL)
  173. {
  174. name = "kernel";
  175. start=flash_base + section.offset;
  176. end=start + section.total_size;
  177. titan_add_partition(name, start, end);
  178. total_sects++;
  179. }
  180. }
  181. /* Next, lets add the single image */
  182. titan_add_partition("primary_image", flash_base, flash_end);
  183. total_sects++;
  184. titan_add_partition("full_image", 0, master->size);
  185. total_sects++;
  186. if (!titan_parse_env_address("BOOTLOADER", &start, &end)){
  187. titan_add_partition("bootloader", start, end);
  188. total_sects++;
  189. }
  190. if (!titan_parse_env_address("boot_env", &start, &end)){
  191. titan_add_partition("boot_env", start, end);
  192. total_sects++;
  193. }
  194. *pparts = titan_parts;
  195. return total_sects;
  196. }