part_iso.c 7.9 KB

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