part_iso.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001
  4. * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch.
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <asm/cache.h>
  9. #include <asm/unaligned.h>
  10. #include "part_iso.h"
  11. #ifdef CONFIG_HAVE_BLOCK_DEVICE
  12. /* #define ISO_PART_DEBUG */
  13. #ifdef ISO_PART_DEBUG
  14. #define PRINTF(fmt,args...) printf (fmt ,##args)
  15. #else
  16. #define PRINTF(fmt,args...)
  17. #endif
  18. /* enable this if CDs are written with the PowerPC Platform ID */
  19. #undef CHECK_FOR_POWERPC_PLATTFORM
  20. #define CD_SECTSIZE 2048
  21. static unsigned char tmpbuf[CD_SECTSIZE] __aligned(ARCH_DMA_MINALIGN);
  22. unsigned long iso_dread(struct blk_desc *block_dev, lbaint_t start,
  23. lbaint_t blkcnt, void *buffer)
  24. {
  25. unsigned long ret;
  26. if (block_dev->blksz == 512) {
  27. /* Convert from 2048 to 512 sector size */
  28. start *= 4;
  29. blkcnt *= 4;
  30. }
  31. ret = blk_dread(block_dev, start, blkcnt, buffer);
  32. if (block_dev->blksz == 512)
  33. ret /= 4;
  34. return ret;
  35. }
  36. /* only boot records will be listed as valid partitions */
  37. int part_get_info_iso_verb(struct blk_desc *dev_desc, int part_num,
  38. struct disk_partition *info, int verb)
  39. {
  40. int i,offset,entry_num;
  41. unsigned short *chksumbuf;
  42. unsigned short chksum;
  43. unsigned long newblkaddr,blkaddr,lastsect,bootaddr;
  44. iso_boot_rec_t *pbr = (iso_boot_rec_t *)tmpbuf; /* boot record */
  45. iso_pri_rec_t *ppr = (iso_pri_rec_t *)tmpbuf; /* primary desc */
  46. iso_val_entry_t *pve = (iso_val_entry_t *)tmpbuf;
  47. iso_init_def_entry_t *pide;
  48. if ((dev_desc->blksz != CD_SECTSIZE) && (dev_desc->blksz != 512))
  49. return -1;
  50. /* the first sector (sector 0x10) must be a primary volume desc */
  51. blkaddr=PVD_OFFSET;
  52. if (iso_dread(dev_desc, PVD_OFFSET, 1, (ulong *)tmpbuf) != 1)
  53. return -1;
  54. if(ppr->desctype!=0x01) {
  55. if(verb)
  56. printf ("** First descriptor is NOT a primary desc on %d:%d **\n",
  57. dev_desc->devnum, part_num);
  58. return (-1);
  59. }
  60. if(strncmp((char *)ppr->stand_ident,"CD001",5)!=0) {
  61. if(verb)
  62. printf ("** Wrong ISO Ident: %s on %d:%d **\n",
  63. ppr->stand_ident, dev_desc->devnum, part_num);
  64. return (-1);
  65. }
  66. lastsect = le32_to_cpu(ppr->firstsek_LEpathtab1_LE);
  67. /* assuming same block size for all entries */
  68. info->blksz = be16_to_cpu(ppr->secsize_BE);
  69. PRINTF(" Lastsect:%08lx\n",lastsect);
  70. for(i=blkaddr;i<lastsect;i++) {
  71. PRINTF("Reading block %d\n", i);
  72. if (iso_dread(dev_desc, i, 1, (ulong *)tmpbuf) != 1)
  73. return -1;
  74. if(ppr->desctype==0x00)
  75. break; /* boot entry found */
  76. if(ppr->desctype==0xff) {
  77. if(verb)
  78. printf ("** No valid boot catalog found on %d:%d **\n",
  79. dev_desc->devnum, part_num);
  80. return (-1);
  81. }
  82. }
  83. /* boot entry found */
  84. if(strncmp(pbr->ident_str,"EL TORITO SPECIFICATION",23)!=0) {
  85. if(verb)
  86. printf ("** Wrong El Torito ident: %s on %d:%d **\n",
  87. pbr->ident_str, dev_desc->devnum, part_num);
  88. return (-1);
  89. }
  90. bootaddr = get_unaligned_le32(pbr->pointer);
  91. PRINTF(" Boot Entry at: %08lX\n",bootaddr);
  92. if (iso_dread(dev_desc, bootaddr, 1, (ulong *)tmpbuf) != 1) {
  93. if(verb)
  94. printf ("** Can't read Boot Entry at %lX on %d:%d **\n",
  95. bootaddr, dev_desc->devnum, part_num);
  96. return (-1);
  97. }
  98. chksum=0;
  99. chksumbuf = (unsigned short *)tmpbuf;
  100. for(i=0;i<0x10;i++)
  101. chksum += le16_to_cpu(chksumbuf[i]);
  102. if(chksum!=0) {
  103. if(verb)
  104. printf("** Checksum Error in booting catalog validation entry on %d:%d **\n",
  105. dev_desc->devnum, part_num);
  106. return (-1);
  107. }
  108. if((pve->key[0]!=0x55)||(pve->key[1]!=0xAA)) {
  109. if(verb)
  110. printf ("** Key 0x55 0xAA error on %d:%d **\n",
  111. dev_desc->devnum, part_num);
  112. return(-1);
  113. }
  114. #ifdef CHECK_FOR_POWERPC_PLATTFORM
  115. if(pve->platform!=0x01) {
  116. if(verb)
  117. printf ("** No PowerPC platform CD on %d:%d **\n",
  118. dev_desc->devnum, part_num);
  119. return(-1);
  120. }
  121. #endif
  122. /* the validation entry seems to be ok, now search the "partition" */
  123. entry_num=1;
  124. offset=0x20;
  125. strcpy((char *)info->type, "U-Boot");
  126. part_set_generic_name(dev_desc, part_num, (char *)info->name);
  127. /* the bootcatalog (including validation Entry) is limited to 2048Bytes
  128. * (63 boot entries + validation entry) */
  129. while(offset<2048) {
  130. pide=(iso_init_def_entry_t *)&tmpbuf[offset];
  131. if ((pide->boot_ind==0x88) ||
  132. (pide->boot_ind==0x00)) { /* Header Id for default Sections Entries */
  133. if(entry_num==part_num) { /* part found */
  134. goto found;
  135. }
  136. entry_num++; /* count partitions Entries (boot and non bootables */
  137. offset+=0x20;
  138. continue;
  139. }
  140. if ((pide->boot_ind==0x90) || /* Section Header Entry */
  141. (pide->boot_ind==0x91) || /* Section Header Entry (last) */
  142. (pide->boot_ind==0x44)) { /* Extension Indicator */
  143. offset+=0x20; /* skip unused entries */
  144. }
  145. else {
  146. if(verb)
  147. printf ("** Partition %d not found on device %d **\n",
  148. part_num, dev_desc->devnum);
  149. return(-1);
  150. }
  151. }
  152. /* if we reach this point entire sector has been
  153. * searched w/o succsess */
  154. if(verb)
  155. printf ("** Partition %d not found on device %d **\n",
  156. part_num, dev_desc->devnum);
  157. return(-1);
  158. found:
  159. if(pide->boot_ind!=0x88) {
  160. if(verb)
  161. printf("** Partition %d is not bootable on device %d **\n",
  162. part_num, dev_desc->devnum);
  163. return (-1);
  164. }
  165. switch(pide->boot_media) {
  166. case 0x00: /* no emulation */
  167. info->size = get_unaligned_le16(pide->sec_cnt)>>2;
  168. break;
  169. case 0x01: info->size=2400>>2; break; /* 1.2MByte Floppy */
  170. case 0x02: info->size=2880>>2; break; /* 1.44MByte Floppy */
  171. case 0x03: info->size=5760>>2; break; /* 2.88MByte Floppy */
  172. case 0x04: info->size=2880>>2; break; /* dummy (HD Emulation) */
  173. default: info->size=0; break;
  174. }
  175. newblkaddr = get_unaligned_le32(pide->rel_block_addr);
  176. info->start=newblkaddr;
  177. if (dev_desc->blksz == 512) {
  178. info->size *= 4;
  179. info->start *= 4;
  180. info->blksz = 512;
  181. }
  182. PRINTF(" part %d found @ %lx size %lx\n",part_num,info->start,info->size);
  183. return 0;
  184. }
  185. static int part_get_info_iso(struct blk_desc *dev_desc, int part_num,
  186. struct disk_partition *info)
  187. {
  188. return part_get_info_iso_verb(dev_desc, part_num, info, 0);
  189. }
  190. static void part_print_iso(struct blk_desc *dev_desc)
  191. {
  192. struct disk_partition info;
  193. int i;
  194. if (part_get_info_iso_verb(dev_desc, 1, &info, 0) == -1) {
  195. printf("** No boot partition found on device %d **\n",
  196. dev_desc->devnum);
  197. return;
  198. }
  199. printf("Part Start Sect x Size Type\n");
  200. i=1;
  201. do {
  202. printf(" %2d " LBAFU " " LBAFU " %6ld %.32s\n",
  203. i, info.start, info.size, info.blksz, info.type);
  204. i++;
  205. } while (part_get_info_iso_verb(dev_desc, i, &info, 0) != -1);
  206. }
  207. static int part_test_iso(struct blk_desc *dev_desc)
  208. {
  209. struct disk_partition info;
  210. return part_get_info_iso_verb(dev_desc, 1, &info, 0);
  211. }
  212. U_BOOT_PART_TYPE(iso) = {
  213. .name = "ISO",
  214. .part_type = PART_TYPE_ISO,
  215. .max_entries = ISO_ENTRY_NUMBERS,
  216. .get_info = part_get_info_iso,
  217. .print = part_print_iso,
  218. .test = part_test_iso,
  219. };
  220. #endif