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