This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
crc32.c
158 lines (145 loc) · 5.25 KB
/
crc32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Op6tSlot Module
* Copyright (C) 2021 longjunyu <ljy122@qq.com>
* Copyright (C) 2021 Junyue Wang <wjyue2001@qq.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/
*/
#include "crc32.h"
#include <Library/UefiLib.h>
EFI_STATUS FixGptCRC32(EFI_BLOCK_IO_PROTOCOL *mBlockIoProtocol,
EFI_DISK_IO_PROTOCOL *mDiskIoProtocol,
EFI_SYSTEM_TABLE *mSystemTable){
EFI_STATUS status;
UINT32 mMediaId;
UINT32 mBlockSize;
mMediaId = mBlockIoProtocol->Media->MediaId;
mBlockSize = mBlockIoProtocol->Media->BlockSize;
unsigned char crc32_header[GPT_CRC32_LEN];
unsigned char crc32_entry[GPT_CRC32_LEN];
unsigned char *bufGptHeader;
unsigned char *bufGptEntry;
// try to allocate pool for bufGptEntry
status = mSystemTable->BootServices->AllocatePool(EfiBootServicesCode, GPT_ENTRY_COUNT * mBlockSize, (VOID**)&bufGptEntry);
if (EFI_ERROR(status)) {
return status;
}
// read gpt entry list
status = mDiskIoProtocol->ReadDisk(mDiskIoProtocol,
mMediaId,
2 * mBlockSize,
GPT_ENTRY_COUNT * mBlockSize,
bufGptEntry);
if (EFI_ERROR(status))
return status;
// get gpt entry crc32 value
get_result_array(calculate_crc32(bufGptEntry, GPT_ENTRY_COUNT * mBlockSize), crc32_entry);
// write gpt entry crc32 value to disk
status = mDiskIoProtocol->WriteDisk(mDiskIoProtocol,
mMediaId,
mBlockSize + GPT_ENTRY_CRC32_LBA1_OFFSET,
GPT_CRC32_LEN,
crc32_entry);
if (EFI_ERROR(status))
return status;
// try to release bufGptEntry
status = mSystemTable->BootServices->FreePool(bufGptEntry);
if (EFI_ERROR(status)) {
return status;
}
// try to allocate pool for bufGptHeader
status = mSystemTable->BootServices->AllocatePool(EfiBootServicesCode, GPT_HEADER_SIZE, (VOID**)&bufGptHeader);
// get gpt header
status = mDiskIoProtocol->ReadDisk(mDiskIoProtocol,
mMediaId,
mBlockSize,
GPT_HEADER_SIZE,
bufGptHeader);
if (EFI_ERROR(status))
return status;
// set previous crc32 value to 0x00
for (int i = GPT_HEADER_CRC32_LBA1_OFFSET; i < GPT_HEADER_CRC32_LBA1_OFFSET + GPT_CRC32_LEN; i++) {
bufGptHeader[i] = 0x00;
}
// get gpt header crc32 value
get_result_array(calculate_crc32(bufGptHeader, GPT_HEADER_SIZE), crc32_header);
// write gpt header crc32 value to disk
status = mDiskIoProtocol->WriteDisk(mDiskIoProtocol,
mMediaId,
mBlockSize + GPT_HEADER_CRC32_LBA1_OFFSET,
GPT_CRC32_LEN,
crc32_header);
if (EFI_ERROR(status))
return status;
// try to release bufGptHeader
status = mSystemTable->BootServices->FreePool(bufGptHeader);
if (EFI_ERROR(status)) {
return status;
}
return EFI_SUCCESS;
}
/*
* A8h reflected is 15h, i.e. 10101000 <--> 00010101
*/
int reflect(int data, int len)
{
int ref = 0;
for (int i = 0; i < len; i++) {
if (data & 0x1) {
ref |= (1 << ((len - 1) - i));
}
data = (data >> 1);
}
return ref;
}
/*
* Function to calculate the CRC32
*/
unsigned int calculate_crc32(unsigned char *buffer, int len)
{
int byte_length = 8; /*length of unit (i.e. byte) */
int msb = 0;
int polynomial = 0x04C11DB7; /* IEEE 32bit polynomial */
unsigned int regs = 0xFFFFFFFF; /* init to all ones */
int regs_mask = 0xFFFFFFFF; /* ensure only 32 bit answer */
int regs_msb = 0;
unsigned int reflected_regs;
for (int i = 0; i < len; i++) {
int data_byte = buffer[i];
data_byte = reflect(data_byte, 8);
for (int j = 0; j < byte_length; j++) {
msb = data_byte >> (byte_length - 1); /* get MSB */
msb &= 1; /* ensure just 1 bit */
regs_msb = (regs >> 31) & 1; /* MSB of regs */
regs = regs << 1; /* shift regs for CRC-CCITT */
if (regs_msb ^ msb) { /* MSB is a 1 */
regs = regs ^ polynomial; /* XOR with generator poly */
}
regs = regs & regs_mask; /* Mask off excess upper bits */
data_byte <<= 1; /* get to next bit */
}
}
regs = regs & regs_mask;
reflected_regs = reflect(regs, 32) ^ 0xFFFFFFFF;
return reflected_regs;
}
// Convert Function
//unsigned char** convert(unsigned int reflected_regs, int *size)
void get_result_array(unsigned int reflected_regs, unsigned char * res)
{
for (int i = 0; i < 4; i++) {
res[i] = reflected_regs & 0xff;
reflected_regs >>= 8;
}
}