]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - vtoycli/vtoygpt.c
Fix the order issue in TreeView mode. (#3218)
[Ventoy.git] / vtoycli / vtoygpt.c
1 /******************************************************************************
2 * vtoygpt.c ---- ventoy gpt util
3 *
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 #include <stdio.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/mman.h>
29 #include <sys/ioctl.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <dirent.h>
33 #include "vtoycli.h"
34
35 void DumpGuid(const char *prefix, GUID *guid)
36 {
37 printf("%s: %08x-%04x-%04x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\n",
38 prefix,
39 guid->data1, guid->data2, guid->data3,
40 guid->data4[0], guid->data4[1], guid->data4[2], guid->data4[3],
41 guid->data4[4], guid->data4[5], guid->data4[6], guid->data4[7]
42 );
43 }
44
45 void DumpHead(VTOY_GPT_HDR *pHead)
46 {
47 UINT32 CrcRead;
48 UINT32 CrcCalc;
49
50 printf("Signature:<%s>\n", pHead->Signature);
51 printf("Version:<%02x %02x %02x %02x>\n", pHead->Version[0], pHead->Version[1], pHead->Version[2], pHead->Version[3]);
52 printf("Length:%u\n", pHead->Length);
53 printf("Crc:0x%08x\n", pHead->Crc);
54 printf("EfiStartLBA:%lu\n", pHead->EfiStartLBA);
55 printf("EfiBackupLBA:%lu\n", pHead->EfiBackupLBA);
56 printf("PartAreaStartLBA:%lu\n", pHead->PartAreaStartLBA);
57 printf("PartAreaEndLBA:%lu\n", pHead->PartAreaEndLBA);
58 DumpGuid("DiskGuid", &pHead->DiskGuid);
59
60 printf("PartTblStartLBA:%lu\n", pHead->PartTblStartLBA);
61 printf("PartTblTotNum:%u\n", pHead->PartTblTotNum);
62 printf("PartTblEntryLen:%u\n", pHead->PartTblEntryLen);
63 printf("PartTblCrc:0x%08x\n", pHead->PartTblCrc);
64
65 CrcRead = pHead->Crc;
66 pHead->Crc = 0;
67 CrcCalc = VtoyCrc32(pHead, pHead->Length);
68
69 if (CrcCalc != CrcRead)
70 {
71 printf("Head CRC Check Failed\n");
72 }
73 else
74 {
75 printf("Head CRC Check SUCCESS [%x] [%x]\n", CrcCalc, CrcRead);
76 }
77
78 CrcRead = pHead->PartTblCrc;
79 CrcCalc = VtoyCrc32(pHead + 1, pHead->PartTblEntryLen * pHead->PartTblTotNum);
80 if (CrcCalc != CrcRead)
81 {
82 printf("Part Table CRC Check Failed\n");
83 }
84 else
85 {
86 printf("Part Table CRC Check SUCCESS [%x] [%x]\n", CrcCalc, CrcRead);
87 }
88 }
89
90 void DumpPartTable(VTOY_GPT_PART_TBL *Tbl)
91 {
92 int i;
93
94 DumpGuid("PartType", &Tbl->PartType);
95 DumpGuid("PartGuid", &Tbl->PartGuid);
96 printf("StartLBA:%lu\n", Tbl->StartLBA);
97 printf("LastLBA:%lu\n", Tbl->LastLBA);
98 printf("Attr:0x%lx\n", Tbl->Attr);
99 printf("Name:");
100
101 for (i = 0; i < 36 && Tbl->Name[i]; i++)
102 {
103 printf("%c", (CHAR)(Tbl->Name[i]));
104 }
105 printf("\n");
106 }
107
108 void DumpMBR(MBR_HEAD *pMBR)
109 {
110 int i;
111
112 for (i = 0; i < 4; i++)
113 {
114 printf("=========== Partition Table %d ============\n", i + 1);
115 printf("PartTbl.Active = 0x%x\n", pMBR->PartTbl[i].Active);
116 printf("PartTbl.FsFlag = 0x%x\n", pMBR->PartTbl[i].FsFlag);
117 printf("PartTbl.StartSectorId = %u\n", pMBR->PartTbl[i].StartSectorId);
118 printf("PartTbl.SectorCount = %u\n", pMBR->PartTbl[i].SectorCount);
119 printf("PartTbl.StartHead = %u\n", pMBR->PartTbl[i].StartHead);
120 printf("PartTbl.StartSector = %u\n", pMBR->PartTbl[i].StartSector);
121 printf("PartTbl.StartCylinder = %u\n", pMBR->PartTbl[i].StartCylinder);
122 printf("PartTbl.EndHead = %u\n", pMBR->PartTbl[i].EndHead);
123 printf("PartTbl.EndSector = %u\n", pMBR->PartTbl[i].EndSector);
124 printf("PartTbl.EndCylinder = %u\n", pMBR->PartTbl[i].EndCylinder);
125 }
126 }
127
128 int DumpGptInfo(VTOY_GPT_INFO *pGptInfo)
129 {
130 int i;
131
132 DumpMBR(&pGptInfo->MBR);
133 DumpHead(&pGptInfo->Head);
134
135 for (i = 0; i < 128; i++)
136 {
137 if (pGptInfo->PartTbl[i].StartLBA == 0)
138 {
139 break;
140 }
141
142 printf("=====Part %d=====\n", i);
143 DumpPartTable(pGptInfo->PartTbl + i);
144 }
145
146 return 0;
147 }
148
149 int vtoygpt_main(int argc, char **argv)
150 {
151 int i;
152 int fd;
153 UINT64 DiskSize;
154 CHAR16 *Name = NULL;
155 VTOY_GPT_INFO *pMainGptInfo = NULL;
156 VTOY_BK_GPT_INFO *pBackGptInfo = NULL;
157
158 if (argc != 3)
159 {
160 printf("usage: vtoygpt -f /dev/sdb\n");
161 return 1;
162 }
163
164 fd = open(argv[2], O_RDWR);
165 if (fd < 0)
166 {
167 printf("Failed to open %s\n", argv[2]);
168 return 1;
169 }
170
171 pMainGptInfo = malloc(sizeof(VTOY_GPT_INFO));
172 pBackGptInfo = malloc(sizeof(VTOY_BK_GPT_INFO));
173 if (NULL == pMainGptInfo || NULL == pBackGptInfo)
174 {
175 close(fd);
176 return 1;
177 }
178
179 read(fd, pMainGptInfo, sizeof(VTOY_GPT_INFO));
180
181 if (argv[1][0] == '-' && argv[1][1] == 'd')
182 {
183 DumpGptInfo(pMainGptInfo);
184 }
185 else
186 {
187 DiskSize = lseek(fd, 0, SEEK_END);
188 lseek(fd, DiskSize - 33 * 512, SEEK_SET);
189 read(fd, pBackGptInfo, sizeof(VTOY_BK_GPT_INFO));
190
191 Name = pMainGptInfo->PartTbl[1].Name;
192 if (Name[0] == 'V' && Name[1] == 'T' && Name[2] == 'O' && Name[3] == 'Y')
193 {
194 if (pMainGptInfo->PartTbl[1].Attr != VENTOY_EFI_PART_ATTR)
195 {
196 pMainGptInfo->PartTbl[1].Attr = VENTOY_EFI_PART_ATTR;
197 pMainGptInfo->Head.PartTblCrc = VtoyCrc32(pMainGptInfo->PartTbl, sizeof(pMainGptInfo->PartTbl));
198 pMainGptInfo->Head.Crc = 0;
199 pMainGptInfo->Head.Crc = VtoyCrc32(&pMainGptInfo->Head, pMainGptInfo->Head.Length);
200
201 pBackGptInfo->PartTbl[1].Attr = VENTOY_EFI_PART_ATTR;
202 pBackGptInfo->Head.PartTblCrc = VtoyCrc32(pBackGptInfo->PartTbl, sizeof(pBackGptInfo->PartTbl));
203 pBackGptInfo->Head.Crc = 0;
204 pBackGptInfo->Head.Crc = VtoyCrc32(&pBackGptInfo->Head, pBackGptInfo->Head.Length);
205
206 lseek(fd, 512, SEEK_SET);
207 write(fd, (UINT8 *)pMainGptInfo + 512, sizeof(VTOY_GPT_INFO) - 512);
208
209 lseek(fd, DiskSize - 33 * 512, SEEK_SET);
210 write(fd, pBackGptInfo, sizeof(VTOY_BK_GPT_INFO));
211
212 fsync(fd);
213 }
214 }
215 }
216
217 free(pMainGptInfo);
218 free(pBackGptInfo);
219 close(fd);
220
221 return 0;
222 }
223