part_iso.c 6.6 KB

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