part_iso.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include "part_iso.h"
  26. #if ((CONFIG_COMMANDS & CFG_CMD_IDE) || \
  27. (CONFIG_COMMANDS & CFG_CMD_SCSI) || \
  28. (CONFIG_COMMANDS & CFG_CMD_USB) || \
  29. defined(CONFIG_MMC) || \
  30. defined(CONFIG_SYSTEMACE) ) && defined(CONFIG_ISO_PARTITION)
  31. /* #define ISO_PART_DEBUG */
  32. #ifdef ISO_PART_DEBUG
  33. #define PRINTF(fmt,args...) printf (fmt ,##args)
  34. #else
  35. #define PRINTF(fmt,args...)
  36. #endif
  37. /* enable this if CDs are written with the PowerPC Platform ID */
  38. #undef CHECK_FOR_POWERPC_PLATTFORM
  39. #define CD_SECTSIZE 2048
  40. static unsigned char tmpbuf[CD_SECTSIZE];
  41. /* Convert char[4] in little endian format to the host format integer
  42. */
  43. static inline unsigned long le32_to_int(unsigned char *le32)
  44. {
  45. return ((le32[3] << 24) +
  46. (le32[2] << 16) +
  47. (le32[1] << 8) +
  48. le32[0]
  49. );
  50. }
  51. /* Convert char[2] in little endian format to the host format integer
  52. */
  53. static inline unsigned short le16_to_int(unsigned char *le16)
  54. {
  55. return ((le16[1] << 8) +
  56. le16[0]
  57. );
  58. }
  59. /* only boot records will be listed as valid partitions */
  60. int get_partition_info_iso_verb(block_dev_desc_t * dev_desc, int part_num, disk_partition_t * info, int verb)
  61. {
  62. int i,offset,entry_num;
  63. unsigned short *chksumbuf;
  64. unsigned short chksum;
  65. unsigned long newblkaddr,blkaddr,lastsect,bootaddr;
  66. iso_boot_rec_t *pbr = (iso_boot_rec_t *)tmpbuf; /* boot record */
  67. iso_pri_rec_t *ppr = (iso_pri_rec_t *)tmpbuf; /* primary desc */
  68. iso_val_entry_t *pve = (iso_val_entry_t *)tmpbuf;
  69. iso_init_def_entry_t *pide;
  70. /* the first sector (sector 0x10) must be a primary volume desc */
  71. blkaddr=PVD_OFFSET;
  72. if (dev_desc->block_read (dev_desc->dev, PVD_OFFSET, 1, (ulong *) tmpbuf) != 1)
  73. return (-1);
  74. if(ppr->desctype!=0x01) {
  75. if(verb)
  76. printf ("** First descriptor is NOT a primary desc on %d:%d **\n",
  77. dev_desc->dev, part_num);
  78. return (-1);
  79. }
  80. if(strncmp(ppr->stand_ident,"CD001",5)!=0) {
  81. if(verb)
  82. printf ("** Wrong ISO Ident: %s on %d:%d **\n",
  83. ppr->stand_ident,dev_desc->dev, part_num);
  84. return (-1);
  85. }
  86. lastsect= ((ppr->firstsek_LEpathtab1_LE & 0x000000ff)<<24) +
  87. ((ppr->firstsek_LEpathtab1_LE & 0x0000ff00)<< 8) +
  88. ((ppr->firstsek_LEpathtab1_LE & 0x00ff0000)>> 8) +
  89. ((ppr->firstsek_LEpathtab1_LE & 0xff000000)>>24) ;
  90. info->blksz=ppr->secsize_BE; /* assuming same block size for all entries */
  91. PRINTF(" Lastsect:%08lx\n",lastsect);
  92. for(i=blkaddr;i<lastsect;i++) {
  93. PRINTF("Reading block %d\n", i);
  94. if (dev_desc->block_read (dev_desc->dev, i, 1, (ulong *) tmpbuf) != 1)
  95. return (-1);
  96. if(ppr->desctype==0x00)
  97. break; /* boot entry found */
  98. if(ppr->desctype==0xff) {
  99. if(verb)
  100. printf ("** No valid boot catalog found on %d:%d **\n",
  101. dev_desc->dev, part_num);
  102. return (-1);
  103. }
  104. }
  105. /* boot entry found */
  106. if(strncmp(pbr->ident_str,"EL TORITO SPECIFICATION",23)!=0) {
  107. if(verb)
  108. printf ("** Wrong El Torito ident: %s on %d:%d **\n",
  109. pbr->ident_str,dev_desc->dev, part_num);
  110. return (-1);
  111. }
  112. bootaddr=le32_to_int(pbr->pointer);
  113. PRINTF(" Boot Entry at: %08lX\n",bootaddr);
  114. if (dev_desc->block_read (dev_desc->dev, bootaddr, 1, (ulong *) tmpbuf) != 1) {
  115. if(verb)
  116. printf ("** Can't read Boot Entry at %lX on %d:%d **\n",
  117. bootaddr,dev_desc->dev, part_num);
  118. return (-1);
  119. }
  120. chksum=0;
  121. chksumbuf = (unsigned short *)tmpbuf;
  122. for(i=0;i<0x10;i++)
  123. chksum+=((chksumbuf[i] &0xff)<<8)+((chksumbuf[i] &0xff00)>>8);
  124. if(chksum!=0) {
  125. if(verb)
  126. printf ("** Checksum Error in booting catalog validation entry on %d:%d **\n",
  127. dev_desc->dev, part_num);
  128. return (-1);
  129. }
  130. if((pve->key[0]!=0x55)||(pve->key[1]!=0xAA)) {
  131. if(verb)
  132. printf ("** Key 0x55 0xAA error on %d:%d **\n",
  133. dev_desc->dev, part_num);
  134. return(-1);
  135. }
  136. #ifdef CHECK_FOR_POWERPC_PLATTFORM
  137. if(pve->platform!=0x01) {
  138. if(verb)
  139. printf ("** No PowerPC platform CD on %d:%d **\n",
  140. dev_desc->dev, part_num);
  141. return(-1);
  142. }
  143. #endif
  144. /* the validation entry seems to be ok, now search the "partition" */
  145. entry_num=0;
  146. offset=0x20;
  147. sprintf (info->type, "U-Boot");
  148. switch(dev_desc->if_type) {
  149. case IF_TYPE_IDE:
  150. case IF_TYPE_ATAPI:
  151. sprintf (info->name, "hd%c%d\n", 'a' + dev_desc->dev, part_num);
  152. break;
  153. case IF_TYPE_SCSI:
  154. sprintf (info->name, "sd%c%d\n", 'a' + dev_desc->dev, part_num);
  155. break;
  156. case IF_TYPE_USB:
  157. sprintf (info->name, "usbd%c%d\n", 'a' + dev_desc->dev, part_num);
  158. break;
  159. case IF_TYPE_DOC:
  160. sprintf (info->name, "docd%c%d\n", 'a' + dev_desc->dev, part_num);
  161. break;
  162. default:
  163. sprintf (info->name, "xx%c%d\n", 'a' + dev_desc->dev, part_num);
  164. break;
  165. }
  166. /* the bootcatalog (including validation Entry) is limited to 2048Bytes
  167. * (63 boot entries + validation entry) */
  168. while(offset<2048) {
  169. pide=(iso_init_def_entry_t *)&tmpbuf[offset];
  170. if ((pide->boot_ind==0x88) ||
  171. (pide->boot_ind==0x00)) { /* Header Id for default Sections Entries */
  172. if(entry_num==part_num) { /* part found */
  173. goto found;
  174. }
  175. entry_num++; /* count partitions Entries (boot and non bootables */
  176. offset+=0x20;
  177. continue;
  178. }
  179. if ((pide->boot_ind==0x90) || /* Section Header Entry */
  180. (pide->boot_ind==0x91) || /* Section Header Entry (last) */
  181. (pide->boot_ind==0x44)) { /* Extension Indicator */
  182. offset+=0x20; /* skip unused entries */
  183. }
  184. else {
  185. if(verb)
  186. printf ("** Partition %d not found on device %d **\n",
  187. part_num,dev_desc->dev);
  188. return(-1);
  189. }
  190. }
  191. /* if we reach this point entire sector has been
  192. * searched w/o succsess */
  193. if(verb)
  194. printf ("** Partition %d not found on device %d **\n",
  195. part_num,dev_desc->dev);
  196. return(-1);
  197. found:
  198. if(pide->boot_ind!=0x88) {
  199. if(verb)
  200. printf ("** Partition %d is not bootable on device %d **\n",
  201. part_num,dev_desc->dev);
  202. return (-1);
  203. }
  204. switch(pide->boot_media) {
  205. case 0x00: /* no emulation */
  206. info->size=le16_to_int(pide->sec_cnt)>>2;
  207. break;
  208. case 0x01: info->size=2400>>2; break; /* 1.2MByte Floppy */
  209. case 0x02: info->size=2880>>2; break; /* 1.44MByte Floppy */
  210. case 0x03: info->size=5760>>2; break; /* 2.88MByte Floppy */
  211. case 0x04: info->size=2880>>2; break; /* dummy (HD Emulation) */
  212. default: info->size=0; break;
  213. }
  214. newblkaddr=le32_to_int(pide->rel_block_addr);
  215. info->start=newblkaddr;
  216. PRINTF(" part %d found @ %lx size %lx\n",part_num,newblkaddr,info->size);
  217. return 0;
  218. }
  219. int get_partition_info_iso(block_dev_desc_t * dev_desc, int part_num, disk_partition_t * info)
  220. {
  221. return(get_partition_info_iso_verb(dev_desc, part_num, info, 1));
  222. }
  223. void print_part_iso(block_dev_desc_t * dev_desc)
  224. {
  225. disk_partition_t info;
  226. int i;
  227. if(get_partition_info_iso_verb(dev_desc,0,&info,0)==-1) {
  228. printf("** No boot partition found on device %d **\n",dev_desc->dev);
  229. return;
  230. }
  231. printf("Part Start Sect x Size Type\n");
  232. i=0;
  233. do {
  234. printf (" %2d %8ld %8ld %6ld %.32s\n",
  235. i, info.start, info.size, info.blksz, info.type);
  236. i++;
  237. } while (get_partition_info_iso_verb(dev_desc,i,&info,0)!=-1);
  238. }
  239. int test_part_iso (block_dev_desc_t *dev_desc)
  240. {
  241. disk_partition_t info;
  242. return(get_partition_info_iso_verb(dev_desc,0,&info,0));
  243. }
  244. #endif /* ((CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI)) && defined(CONFIG_ISO_PARTITION) */