Add DiskIo2 protocol definition to MdePkg.
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / PartitionDxe / ElTorito.c
CommitLineData
adbcbf8f 1/** @file\r
2 Decode an El Torito formatted CD-ROM\r
3\r
493d8e3a 4Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 5This program and the accompanying materials\r
f42be642 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
adbcbf8f 12\r
13**/\r
14\r
15\r
16#include "Partition.h"\r
17\r
18\r
19/**\r
20 Install child handles if the Handle supports El Torito format.\r
21\r
22 @param[in] This Calling context.\r
490b5ea1 23 @param[in] Handle Parent Handle.\r
24 @param[in] DiskIo Parent DiskIo interface.\r
493d8e3a 25 @param[in] DiskIo2 Parent DiskIo2 interface.\r
490b5ea1 26 @param[in] BlockIo Parent BlockIo interface.\r
27 @param[in] BlockIo2 Parent BlockIo2 interface.\r
adbcbf8f 28 @param[in] DevicePath Parent Device Path\r
29\r
30\r
490b5ea1 31 @retval EFI_SUCCESS Child handle(s) was added.\r
32 @retval EFI_MEDIA_CHANGED Media changed Detected.\r
33 @retval other no child handle was added.\r
adbcbf8f 34\r
35**/\r
36EFI_STATUS\r
37PartitionInstallElToritoChildHandles (\r
38 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
39 IN EFI_HANDLE Handle,\r
40 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
493d8e3a 41 IN EFI_DISK_IO2_PROTOCOL *DiskIo2,\r
adbcbf8f 42 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
490b5ea1 43 IN EFI_BLOCK_IO2_PROTOCOL *BlockIo2,\r
adbcbf8f 44 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
45 )\r
46{\r
47 EFI_STATUS Status;\r
48 UINT32 VolDescriptorLba;\r
49 UINT32 Lba;\r
50 EFI_BLOCK_IO_MEDIA *Media;\r
51 CDROM_VOLUME_DESCRIPTOR *VolDescriptor;\r
52 ELTORITO_CATALOG *Catalog;\r
53 UINTN Check;\r
54 UINTN Index;\r
55 UINTN BootEntry;\r
56 UINTN MaxIndex;\r
57 UINT16 *CheckBuffer;\r
58 CDROM_DEVICE_PATH CdDev;\r
59 UINT32 SubBlockSize;\r
60 UINT32 SectorCount;\r
61 EFI_STATUS Found;\r
62 UINT32 VolSpaceSize;\r
63\r
64 Found = EFI_NOT_FOUND;\r
65 Media = BlockIo->Media;\r
490b5ea1 66\r
adbcbf8f 67 VolSpaceSize = 0;\r
68\r
69 //\r
70 // CD_ROM has the fixed block size as 2048 bytes\r
71 //\r
72 if (Media->BlockSize != 2048) {\r
73 return EFI_NOT_FOUND;\r
74 }\r
75\r
76 VolDescriptor = AllocatePool ((UINTN) Media->BlockSize);\r
77\r
78 if (VolDescriptor == NULL) {\r
79 return EFI_NOT_FOUND;\r
80 }\r
81\r
82 Catalog = (ELTORITO_CATALOG *) VolDescriptor;\r
83\r
84 //\r
85 // the ISO-9660 volume descriptor starts at 32k on the media\r
86 // and CD_ROM has the fixed block size as 2048 bytes, so...\r
87 //\r
88 //\r
89 // ((16*2048) / Media->BlockSize) - 1;\r
90 //\r
91 VolDescriptorLba = 15;\r
92 //\r
93 // Loop: handle one volume descriptor per time\r
94 //\r
95 while (TRUE) {\r
96\r
97 VolDescriptorLba += 1;\r
98 if (VolDescriptorLba > Media->LastBlock) {\r
99 //\r
100 // We are pointing past the end of the device so exit\r
101 //\r
102 break;\r
103 }\r
104\r
96f99e1d 105 Status = DiskIo->ReadDisk (\r
106 DiskIo,\r
107 Media->MediaId,\r
7059dad9 108 MultU64x32 (VolDescriptorLba, Media->BlockSize),\r
96f99e1d 109 Media->BlockSize,\r
110 VolDescriptor\r
111 );\r
adbcbf8f 112 if (EFI_ERROR (Status)) {\r
113 Found = Status;\r
114 break;\r
115 }\r
116 //\r
117 // Check for valid volume descriptor signature\r
118 //\r
88a19367 119 if (VolDescriptor->Unknown.Type == CDVOL_TYPE_END ||\r
120 CompareMem (VolDescriptor->Unknown.Id, CDVOL_ID, sizeof (VolDescriptor->Unknown.Id)) != 0\r
adbcbf8f 121 ) {\r
122 //\r
123 // end of Volume descriptor list\r
124 //\r
125 break;\r
126 }\r
127 //\r
128 // Read the Volume Space Size from Primary Volume Descriptor 81-88 byte,\r
129 // the 32-bit numerical values is stored in Both-byte orders\r
130 //\r
88a19367 131 if (VolDescriptor->PrimaryVolume.Type == CDVOL_TYPE_CODED) {\r
132 VolSpaceSize = VolDescriptor->PrimaryVolume.VolSpaceSize[0];\r
adbcbf8f 133 }\r
134 //\r
135 // Is it an El Torito volume descriptor?\r
136 //\r
88a19367 137 if (CompareMem (VolDescriptor->BootRecordVolume.SystemId, CDVOL_ELTORITO_ID, sizeof (CDVOL_ELTORITO_ID) - 1) != 0) {\r
adbcbf8f 138 continue;\r
139 }\r
140 //\r
141 // Read in the boot El Torito boot catalog\r
142 //\r
88a19367 143 Lba = UNPACK_INT32 (VolDescriptor->BootRecordVolume.EltCatalog);\r
adbcbf8f 144 if (Lba > Media->LastBlock) {\r
145 continue;\r
146 }\r
147\r
96f99e1d 148 Status = DiskIo->ReadDisk (\r
149 DiskIo,\r
150 Media->MediaId,\r
7059dad9 151 MultU64x32 (Lba, Media->BlockSize),\r
96f99e1d 152 Media->BlockSize,\r
153 Catalog\r
154 );\r
adbcbf8f 155 if (EFI_ERROR (Status)) {\r
156 DEBUG ((EFI_D_ERROR, "EltCheckDevice: error reading catalog %r\n", Status));\r
157 continue;\r
158 }\r
159 //\r
160 // We don't care too much about the Catalog header's contents, but we do want\r
161 // to make sure it looks like a Catalog header\r
162 //\r
163 if (Catalog->Catalog.Indicator != ELTORITO_ID_CATALOG || Catalog->Catalog.Id55AA != 0xAA55) {\r
164 DEBUG ((EFI_D_ERROR, "EltCheckBootCatalog: El Torito boot catalog header IDs not correct\n"));\r
165 continue;\r
166 }\r
167\r
168 Check = 0;\r
169 CheckBuffer = (UINT16 *) Catalog;\r
170 for (Index = 0; Index < sizeof (ELTORITO_CATALOG) / sizeof (UINT16); Index += 1) {\r
171 Check += CheckBuffer[Index];\r
172 }\r
173\r
ea7cb08c 174 if ((Check & 0xFFFF) != 0) {\r
adbcbf8f 175 DEBUG ((EFI_D_ERROR, "EltCheckBootCatalog: El Torito boot catalog header checksum failed\n"));\r
176 continue;\r
177 }\r
178\r
179 MaxIndex = Media->BlockSize / sizeof (ELTORITO_CATALOG);\r
180 for (Index = 1, BootEntry = 1; Index < MaxIndex; Index += 1) {\r
181 //\r
182 // Next entry\r
183 //\r
184 Catalog += 1;\r
185\r
186 //\r
187 // Check this entry\r
188 //\r
189 if (Catalog->Boot.Indicator != ELTORITO_ID_SECTION_BOOTABLE || Catalog->Boot.Lba == 0) {\r
190 continue;\r
191 }\r
192\r
193 SubBlockSize = 512;\r
194 SectorCount = Catalog->Boot.SectorCount;\r
195\r
196 switch (Catalog->Boot.MediaType) {\r
197\r
198 case ELTORITO_NO_EMULATION:\r
199 SubBlockSize = Media->BlockSize;\r
200 break;\r
201\r
202 case ELTORITO_HARD_DISK:\r
203 break;\r
204\r
205 case ELTORITO_12_DISKETTE:\r
206 SectorCount = 0x50 * 0x02 * 0x0F;\r
207 break;\r
208\r
209 case ELTORITO_14_DISKETTE:\r
210 SectorCount = 0x50 * 0x02 * 0x12;\r
211 break;\r
212\r
213 case ELTORITO_28_DISKETTE:\r
214 SectorCount = 0x50 * 0x02 * 0x24;\r
215 break;\r
216\r
217 default:\r
218 DEBUG ((EFI_D_INIT, "EltCheckDevice: unsupported El Torito boot media type %x\n", Catalog->Boot.MediaType));\r
219 SectorCount = 0;\r
220 SubBlockSize = Media->BlockSize;\r
221 break;\r
222 }\r
223 //\r
224 // Create child device handle\r
225 //\r
226 CdDev.Header.Type = MEDIA_DEVICE_PATH;\r
227 CdDev.Header.SubType = MEDIA_CDROM_DP;\r
228 SetDevicePathNodeLength (&CdDev.Header, sizeof (CdDev));\r
229\r
230 if (Index == 1) {\r
231 //\r
232 // This is the initial/default entry\r
233 //\r
234 BootEntry = 0;\r
235 }\r
236\r
237 CdDev.BootEntry = (UINT32) BootEntry;\r
238 BootEntry++;\r
239 CdDev.PartitionStart = Catalog->Boot.Lba;\r
240 if (SectorCount < 2) {\r
241 //\r
242 // When the SectorCount < 2, set the Partition as the whole CD.\r
243 //\r
244 if (VolSpaceSize > (Media->LastBlock + 1)) {\r
245 CdDev.PartitionSize = (UINT32)(Media->LastBlock - Catalog->Boot.Lba + 1);\r
246 } else {\r
247 CdDev.PartitionSize = (UINT32)(VolSpaceSize - Catalog->Boot.Lba);\r
248 }\r
249 } else {\r
250 CdDev.PartitionSize = DivU64x32 (\r
251 MultU64x32 (\r
252 SectorCount,\r
253 SubBlockSize\r
254 ) + Media->BlockSize - 1,\r
255 Media->BlockSize\r
256 );\r
257 }\r
258\r
259 Status = PartitionInstallChildHandle (\r
260 This,\r
261 Handle,\r
262 DiskIo,\r
493d8e3a 263 DiskIo2,\r
adbcbf8f 264 BlockIo,\r
490b5ea1 265 BlockIo2,\r
adbcbf8f 266 DevicePath,\r
267 (EFI_DEVICE_PATH_PROTOCOL *) &CdDev,\r
268 Catalog->Boot.Lba,\r
269 Catalog->Boot.Lba + CdDev.PartitionSize - 1,\r
270 SubBlockSize,\r
271 FALSE\r
272 );\r
273 if (!EFI_ERROR (Status)) {\r
274 Found = EFI_SUCCESS;\r
275 }\r
276 }\r
277 }\r
278\r
279 FreePool (VolDescriptor);\r
280\r
281 return Found;\r
282}\r