]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Disk/Partition/Dxe/ElTorito.c
added PPI and Protocol definitions needed by porting modules
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / Partition / Dxe / ElTorito.c
CommitLineData
b9575d60
A
1/** @file\r
2 Decode an El Torito formatted CD-ROM\r
79840ee1 3\r
b9575d60
A
4 Copyright (c) 2006 - 2007, Intel Corporation \r
5 All rights reserved. This program and the accompanying materials \r
6 are licensed and made available under the terms and conditions of the BSD License \r
7 which accompanies this distribution. The full text of the license may be found at \r
8 http://opensource.org/licenses/bsd-license.php \r
79840ee1 9\r
b9575d60
A
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
79840ee1 12\r
b9575d60 13**/\r
79840ee1 14\r
79840ee1 15\r
d8a43975 16//\r
17// Include common header file for this module.\r
18//\r
19#include "CommonHeader.h"\r
20\r
b9575d60 21#include "Partition.h"\r
79840ee1 22\r
79840ee1 23\r
b9575d60
A
24/**\r
25 Install child handles if the Handle supports El Torito format.\r
79840ee1 26\r
b9575d60
A
27 @param[in] This Calling context.\r
28 @param[in] Handle Parent Handle\r
29 @param[in] DiskIo Parent DiskIo interface\r
30 @param[in] BlockIo Parent BlockIo interface\r
31 @param[in] DevicePath Parent Device Path\r
79840ee1 32\r
79840ee1 33\r
b9575d60
A
34 @retval EFI_SUCCESS Child handle(s) was added\r
35 @retval EFI_MEDIA_CHANGED Media changed Detected\r
36 @retval other no child handle was added\r
79840ee1 37\r
b9575d60 38**/\r
79840ee1 39EFI_STATUS\r
40PartitionInstallElToritoChildHandles (\r
41 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
42 IN EFI_HANDLE Handle,\r
43 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
44 IN EFI_BLOCK_IO_PROTOCOL *BlockIo,\r
45 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
46 )\r
79840ee1 47{\r
48 EFI_STATUS Status;\r
49 UINT32 VolDescriptorLba;\r
50 UINT32 Lba;\r
51 EFI_BLOCK_IO_MEDIA *Media;\r
52 CDROM_VOLUME_DESCRIPTOR *VolDescriptor;\r
53 ELTORITO_CATALOG *Catalog;\r
54 UINTN Check;\r
55 UINTN Index;\r
56 UINTN BootEntry;\r
57 UINTN MaxIndex;\r
58 UINT16 *CheckBuffer;\r
59 CDROM_DEVICE_PATH CdDev;\r
60 UINT32 SubBlockSize;\r
61 UINT32 SectorCount;\r
62 EFI_STATUS Found;\r
63 UINT32 VolSpaceSize;\r
64\r
65 Found = EFI_NOT_FOUND;\r
66 Media = BlockIo->Media;\r
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
105 Status = BlockIo->ReadBlocks (\r
106 BlockIo,\r
107 Media->MediaId,\r
108 VolDescriptorLba,\r
109 Media->BlockSize,\r
110 VolDescriptor\r
111 );\r
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
119 if (VolDescriptor->Type == CDVOL_TYPE_END ||\r
120 CompareMem (VolDescriptor->Id, CDVOL_ID, sizeof (VolDescriptor->Id)) != 0\r
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
131 if (VolDescriptor->Type == CDVOL_TYPE_CODED) {\r
132 VolSpaceSize = VolDescriptor->VolSpaceSize[0];\r
133 }\r
134 //\r
135 // Is it an El Torito volume descriptor?\r
136 //\r
137 if (CompareMem (VolDescriptor->SystemId, CDVOL_ELTORITO_ID, sizeof (CDVOL_ELTORITO_ID) - 1) != 0) {\r
138 continue;\r
139 }\r
140 //\r
141 // Read in the boot El Torito boot catalog\r
142 //\r
143 Lba = UNPACK_INT32 (VolDescriptor->EltCatalog);\r
144 if (Lba > Media->LastBlock) {\r
145 continue;\r
146 }\r
147\r
148 Status = BlockIo->ReadBlocks (\r
149 BlockIo,\r
150 Media->MediaId,\r
151 Lba,\r
152 Media->BlockSize,\r
153 Catalog\r
154 );\r
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
174 if (Check & 0xFFFF) {\r
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
263 BlockIo,\r
264 DevicePath,\r
265 (EFI_DEVICE_PATH_PROTOCOL *) &CdDev,\r
266 Catalog->Boot.Lba,\r
267 Catalog->Boot.Lba + CdDev.PartitionSize - 1,\r
268 SubBlockSize,\r
269 FALSE\r
270 );\r
271 if (!EFI_ERROR (Status)) {\r
272 Found = EFI_SUCCESS;\r
273 }\r
274 }\r
275 }\r
276\r
277 FreePool (VolDescriptor);\r
278\r
279 return Found;\r
280}\r