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