]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/FlashMap/Microcode.c
3 Copyright (c) 2004-2006 Intel Corporation. All rights reserved
4 This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Utility for working with microcode patch files in the Intel
19 Platform Innovation Framework for EFI build environment.
24 #include <string.h> // for memset()
26 #include <stdlib.h> // for malloc()
28 #include "EfiUtilityMsgs.h"
29 #include "Microcode.h"
31 #define MAX_LINE_LEN 256
34 // Structure definition for a microcode header
37 unsigned int HeaderVersion
;
41 unsigned int Checksum
;
42 unsigned int LoaderVersion
;
43 unsigned int PlatformId
;
44 unsigned int DataSize
; // if 0, then TotalSize = 2048, and TotalSize field is invalid
45 unsigned int TotalSize
; // number of bytes
46 unsigned int Reserved
[3];
47 } MICROCODE_IMAGE_HEADER
;
57 MicrocodeConstructor (
64 Constructor of module Microcode
86 Destructor of module Microcode
109 Read a 32-bit microcode data value from a text file and convert to raw binary form.
112 InFptr - file pointer to input text file
113 Data - pointer to where to return the data parsed
116 STATUS_SUCCESS - no errors or warnings, Data contains valid information
117 STATUS_ERROR - errors were encountered
121 char Line
[MAX_LINE_LEN
];
124 Line
[MAX_LINE_LEN
- 1] = 0;
126 if (fgets (Line
, MAX_LINE_LEN
, InFptr
) == NULL
) {
130 // If it was a binary file, then it may have overwritten our null terminator
132 if (Line
[MAX_LINE_LEN
- 1] != 0) {
137 // dd 000000001h ; comment
142 for (cptr
= Line
; *cptr
&& isspace(*cptr
); cptr
++) {
144 if ((tolower(cptr
[0]) == 'd') && (tolower(cptr
[1]) == 'd') && isspace (cptr
[2])) {
146 // Skip blanks and look for a hex digit
149 for (; *cptr
&& isspace(*cptr
); cptr
++) {
151 if (isxdigit (*cptr
)) {
152 if (sscanf (cptr
, "%X", Data
) != 1) {
156 return STATUS_SUCCESS
;
169 Parse a microcode text file, and write the binary results to an output file.
172 InFileName - input text file to parse
173 OutFileName - output file to write raw binary data from parsed input file
176 STATUS_SUCCESS - no errors or warnings
177 STATUS_ERROR - errors were encountered
184 MICROCODE_IMAGE_HEADER
*Header
;
188 unsigned int Checksum
;
191 unsigned int TotalSize
;
193 Status
= STATUS_ERROR
;
198 // Open the input text file
200 if ((InFptr
= fopen (InFileName
, "r")) == NULL
) {
201 Error (NULL
, 0, 0, InFileName
, "failed to open input microcode file for reading");
205 // Make two passes on the input file. The first pass is to determine how
206 // much data is in the file so we can allocate a working buffer. Then
207 // we'll allocate a buffer and re-read the file into the buffer for processing.
211 Status
= MicrocodeReadData (InFptr
, &Data
);
212 if (Status
== STATUS_SUCCESS
) {
213 Size
+= sizeof (Data
);
215 } while (Status
== STATUS_SUCCESS
);
220 Error (NULL
, 0, 0, InFileName
, "no parse-able data found in file");
223 if (Size
< sizeof (MICROCODE_IMAGE_HEADER
)) {
224 Error (NULL
, 0, 0, InFileName
, "amount of parse-able data is insufficient to contain a microcode header");
228 // Allocate a buffer for the data
230 Buffer
= (char *) _malloc (Size
);
231 if (Buffer
== NULL
) {
232 Error (NULL
, 0, 0, "memory allocation failure", NULL
);
236 // Re-read the file, storing the data into our buffer
238 fseek (InFptr
, 0, SEEK_SET
);
241 Status
= MicrocodeReadData (InFptr
, &Data
);
242 if (Status
== STATUS_SUCCESS
) {
243 *(unsigned int *) Ptr
= Data
;
244 Ptr
+= sizeof (Data
);
246 } while (Status
== STATUS_SUCCESS
);
248 // Can't do much checking on the header because, per the spec, the
249 // DataSize field may be 0, which means DataSize = 2000 and TotalSize = 2K,
250 // and the TotalSize field is invalid (actually missing). Thus we can't
251 // even verify the Reserved fields are 0.
253 Header
= (MICROCODE_IMAGE_HEADER
*) Buffer
;
254 if (Header
->DataSize
== 0) {
257 TotalSize
= Header
->TotalSize
;
259 if (TotalSize
!= Size
) {
260 Error (NULL
, 0, 0, InFileName
, "file contents do not contain expected TotalSize 0x%04X", TotalSize
);
264 // Checksum the contents
269 while (Size2
< Size
) {
270 Checksum
+= *(unsigned int *) Ptr
;
275 Error (NULL
, 0, 0, InFileName
, "checksum failed on file contents");
279 // Open the output file and write the buffer contents
281 if ((OutFptr
= fopen (OutFileName
, "wb")) == NULL
) {
282 Error (NULL
, 0, 0, OutFileName
, "failed to open output microcode file for writing");
285 if (fwrite (Buffer
, Size
, 1, OutFptr
) != 1) {
286 Error (NULL
, 0, 0, OutFileName
, "failed to write microcode data to output file");
289 Status
= STATUS_SUCCESS
;
291 if (Buffer
!= NULL
) {
294 if (InFptr
!= NULL
) {
297 if (OutFptr
!= NULL
) {
299 if (Status
== STATUS_ERROR
) {
300 remove (OutFileName
);