]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/Ventoy2Disk/Core/ventoy_util.c
1.0.65 release
[Ventoy.git] / LinuxGUI / Ventoy2Disk / Core / ventoy_util.c
1 /******************************************************************************
2 * ventoy_util.c ---- ventoy util
3 * Copyright (c) 2021, longpanda <admin@ventoy.net>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <errno.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <sys/mount.h>
33 #include <linux/fs.h>
34 #include <dirent.h>
35 #include <time.h>
36 #include <ventoy_define.h>
37 #include <ventoy_util.h>
38
39 uint8_t g_mbr_template[512];
40
41 void ventoy_gen_preudo_uuid(void *uuid)
42 {
43 int i;
44 int fd;
45
46 fd = open("/dev/urandom", O_RDONLY | O_BINARY);
47 if (fd < 0)
48 {
49 srand(time(NULL));
50 for (i = 0; i < 8; i++)
51 {
52 *((uint16_t *)uuid + i) = (uint16_t)(rand() & 0xFFFF);
53 }
54 }
55 else
56 {
57 read(fd, uuid, 16);
58 close(fd);
59 }
60 }
61
62 uint64_t ventoy_get_human_readable_gb(uint64_t SizeBytes)
63 {
64 int i;
65 int Pow2 = 1;
66 double Delta;
67 double GB = SizeBytes * 1.0 / 1000 / 1000 / 1000;
68
69 if ((SizeBytes % SIZE_1GB) == 0)
70 {
71 return (uint64_t)(SizeBytes / SIZE_1GB);
72 }
73
74 for (i = 0; i < 12; i++)
75 {
76 if (Pow2 > GB)
77 {
78 Delta = (Pow2 - GB) / Pow2;
79 }
80 else
81 {
82 Delta = (GB - Pow2) / Pow2;
83 }
84
85 if (Delta < 0.05)
86 {
87 return Pow2;
88 }
89
90 Pow2 <<= 1;
91 }
92
93 return (uint64_t)GB;
94 }
95
96
97 int ventoy_get_sys_file_line(char *buffer, int buflen, const char *fmt, ...)
98 {
99 int len;
100 char c;
101 char path[256];
102 va_list arg;
103
104 va_start(arg, fmt);
105 vsnprintf(path, 256, fmt, arg);
106 va_end(arg);
107
108 if (access(path, F_OK) >= 0)
109 {
110 FILE *fp = fopen(path, "r");
111 memset(buffer, 0, buflen);
112 len = (int)fread(buffer, 1, buflen - 1, fp);
113 fclose(fp);
114
115 while (len > 0)
116 {
117 c = buffer[len - 1];
118 if (c == '\r' || c == '\n' || c == ' ' || c == '\t')
119 {
120 buffer[len - 1] = 0;
121 len--;
122 }
123 else
124 {
125 break;
126 }
127 }
128
129 return 0;
130 }
131 else
132 {
133 vdebug("%s not exist \n", path);
134 return 1;
135 }
136 }
137
138 int ventoy_is_disk_mounted(const char *devpath)
139 {
140 int len;
141 int mount = 0;
142 char line[512];
143 FILE *fp = NULL;
144
145 fp = fopen("/proc/mounts", "r");
146 if (!fp)
147 {
148 return 0;
149 }
150
151 len = (int)strlen(devpath);
152 while (fgets(line, sizeof(line), fp))
153 {
154 if (strncmp(line, devpath, len) == 0)
155 {
156 mount = 1;
157 vdebug("%s mounted <%s>\n", devpath, line);
158 goto end;
159 }
160 }
161
162 end:
163 fclose(fp);
164 return mount;
165 }
166
167 int ventoy_try_umount_disk(const char *devpath)
168 {
169 int rc;
170 int len;
171 char line[512];
172 char *pos1 = NULL;
173 char *pos2 = NULL;
174 FILE *fp = NULL;
175
176 fp = fopen("/proc/mounts", "r");
177 if (!fp)
178 {
179 return 0;
180 }
181
182 len = (int)strlen(devpath);
183 while (fgets(line, sizeof(line), fp))
184 {
185 if (strncmp(line, devpath, len) == 0)
186 {
187 pos1 = strchr(line, ' ');
188 if (pos1)
189 {
190 pos2 = strchr(pos1 + 1, ' ');
191 if (pos2)
192 {
193 *pos2 = 0;
194 }
195
196 rc = umount(pos1 + 1);
197 if (rc)
198 {
199 vdebug("umount %s %s [ failed ] error:%d\n", devpath, pos1 + 1, errno);
200 }
201 else
202 {
203 vdebug("umount %s %s [ success ]\n", devpath, pos1 + 1);
204 }
205 }
206 }
207 }
208
209 fclose(fp);
210 return 0;
211 }
212
213
214 int ventoy_read_file_to_buf(const char *FileName, int ExtLen, void **Bufer, int *BufLen)
215 {
216 int FileSize;
217 FILE *fp = NULL;
218 void *Data = NULL;
219
220 fp = fopen(FileName, "rb");
221 if (fp == NULL)
222 {
223 vlog("Failed to open file %s", FileName);
224 return 1;
225 }
226
227 fseek(fp, 0, SEEK_END);
228 FileSize = (int)ftell(fp);
229
230 Data = malloc(FileSize + ExtLen);
231 if (!Data)
232 {
233 fclose(fp);
234 return 1;
235 }
236
237 fseek(fp, 0, SEEK_SET);
238 fread(Data, 1, FileSize, fp);
239
240 fclose(fp);
241
242 *Bufer = Data;
243 *BufLen = FileSize;
244
245 return 0;
246 }
247
248 const char * ventoy_get_local_version(void)
249 {
250 int rc;
251 int FileSize;
252 char *Pos = NULL;
253 char *Buf = NULL;
254 static char LocalVersion[64] = { 0 };
255
256 if (LocalVersion[0] == 0)
257 {
258 rc = ventoy_read_file_to_buf("ventoy/version", 1, (void **)&Buf, &FileSize);
259 if (rc)
260 {
261 return "";
262 }
263 Buf[FileSize] = 0;
264
265 for (Pos = Buf; *Pos; Pos++)
266 {
267 if (*Pos == '\r' || *Pos == '\n')
268 {
269 *Pos = 0;
270 break;
271 }
272 }
273
274 scnprintf(LocalVersion, "%s", Buf);
275 free(Buf);
276 }
277
278 return LocalVersion;
279 }
280
281 int VentoyGetLocalBootImg(MBR_HEAD *pMBR)
282 {
283 memcpy(pMBR, g_mbr_template, 512);
284 return 0;
285 }
286
287 static int VentoyFillProtectMBR(uint64_t DiskSizeBytes, MBR_HEAD *pMBR)
288 {
289 ventoy_guid Guid;
290 uint32_t DiskSignature;
291 uint64_t DiskSectorCount;
292
293 VentoyGetLocalBootImg(pMBR);
294
295 ventoy_gen_preudo_uuid(&Guid);
296
297 memcpy(&DiskSignature, &Guid, sizeof(uint32_t));
298
299 vdebug("Disk signature: 0x%08x\n", DiskSignature);
300
301 memcpy(pMBR->BootCode + 0x1B8, &DiskSignature, 4);
302 memcpy(pMBR->BootCode + 0x180, &Guid, 16);
303
304 DiskSectorCount = DiskSizeBytes / 512 - 1;
305 if (DiskSectorCount > 0xFFFFFFFF)
306 {
307 DiskSectorCount = 0xFFFFFFFF;
308 }
309
310 memset(pMBR->PartTbl, 0, sizeof(pMBR->PartTbl));
311
312 pMBR->PartTbl[0].Active = 0x00;
313 pMBR->PartTbl[0].FsFlag = 0xee; // EE
314
315 pMBR->PartTbl[0].StartHead = 0;
316 pMBR->PartTbl[0].StartSector = 1;
317 pMBR->PartTbl[0].StartCylinder = 0;
318 pMBR->PartTbl[0].EndHead = 254;
319 pMBR->PartTbl[0].EndSector = 63;
320 pMBR->PartTbl[0].EndCylinder = 1023;
321
322 pMBR->PartTbl[0].StartSectorId = 1;
323 pMBR->PartTbl[0].SectorCount = (uint32_t)DiskSectorCount;
324
325 pMBR->Byte55 = 0x55;
326 pMBR->ByteAA = 0xAA;
327
328 pMBR->BootCode[92] = 0x22;
329
330 return 0;
331 }
332
333 static int ventoy_fill_gpt_partname(uint16_t Name[36], const char *asciiName)
334 {
335 int i;
336 int len;
337
338 memset(Name, 0, 36 * sizeof(uint16_t));
339 len = (int)strlen(asciiName);
340 for (i = 0; i < 36 && i < len; i++)
341 {
342 Name[i] = asciiName[i];
343 }
344
345 return 0;
346 }
347
348 int ventoy_fill_gpt(uint64_t size, uint64_t reserve, int align4k, VTOY_GPT_INFO *gpt)
349 {
350 uint64_t ReservedSector = 33;
351 uint64_t ModSectorCount = 0;
352 uint64_t Part1SectorCount = 0;
353 uint64_t DiskSectorCount = size / 512;
354 VTOY_GPT_HDR *Head = &gpt->Head;
355 VTOY_GPT_PART_TBL *Table = gpt->PartTbl;
356 ventoy_guid WindowsDataPartType = { 0xebd0a0a2, 0xb9e5, 0x4433, { 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7 } };
357 //ventoy_guid EspPartType = { 0xc12a7328, 0xf81f, 0x11d2, { 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b } };
358 //ventoy_guid BiosGrubPartType = { 0x21686148, 0x6449, 0x6e6f, { 0x74, 0x4e, 0x65, 0x65, 0x64, 0x45, 0x46, 0x49 } };
359
360 VentoyFillProtectMBR(size, &gpt->MBR);
361
362 if (reserve > 0)
363 {
364 ReservedSector += reserve / 512;
365 }
366
367 Part1SectorCount = DiskSectorCount - ReservedSector - (VTOYEFI_PART_BYTES / 512) - 2048;
368
369 ModSectorCount = (Part1SectorCount % 8);
370 if (ModSectorCount)
371 {
372 vlog("Part1SectorCount:%llu is not aligned by 4KB (%llu)\n", (_ull)Part1SectorCount, (_ull)ModSectorCount);
373 }
374
375 // check aligned with 4KB
376 if (align4k)
377 {
378 if (ModSectorCount)
379 {
380 vdebug("Disk need to align with 4KB %u\n", (uint32_t)ModSectorCount);
381 Part1SectorCount -= ModSectorCount;
382 }
383 else
384 {
385 vdebug("no need to align with 4KB\n");
386 }
387 }
388
389 memcpy(Head->Signature, "EFI PART", 8);
390 Head->Version[2] = 0x01;
391 Head->Length = 92;
392 Head->Crc = 0;
393 Head->EfiStartLBA = 1;
394 Head->EfiBackupLBA = DiskSectorCount - 1;
395 Head->PartAreaStartLBA = 34;
396 Head->PartAreaEndLBA = DiskSectorCount - 34;
397 ventoy_gen_preudo_uuid(&Head->DiskGuid);
398 Head->PartTblStartLBA = 2;
399 Head->PartTblTotNum = 128;
400 Head->PartTblEntryLen = 128;
401
402
403 memcpy(&(Table[0].PartType), &WindowsDataPartType, sizeof(ventoy_guid));
404 ventoy_gen_preudo_uuid(&(Table[0].PartGuid));
405 Table[0].StartLBA = 2048;
406 Table[0].LastLBA = 2048 + Part1SectorCount - 1;
407 Table[0].Attr = 0;
408 ventoy_fill_gpt_partname(Table[0].Name, "Ventoy");
409
410 // to fix windows issue
411 //memcpy(&(Table[1].PartType), &EspPartType, sizeof(GUID));
412 memcpy(&(Table[1].PartType), &WindowsDataPartType, sizeof(ventoy_guid));
413 ventoy_gen_preudo_uuid(&(Table[1].PartGuid));
414 Table[1].StartLBA = Table[0].LastLBA + 1;
415 Table[1].LastLBA = Table[1].StartLBA + VTOYEFI_PART_BYTES / 512 - 1;
416 Table[1].Attr = 0xC000000000000001ULL;
417 ventoy_fill_gpt_partname(Table[1].Name, "VTOYEFI");
418
419 #if 0
420 memcpy(&(Table[2].PartType), &BiosGrubPartType, sizeof(ventoy_guid));
421 ventoy_gen_preudo_uuid(&(Table[2].PartGuid));
422 Table[2].StartLBA = 34;
423 Table[2].LastLBA = 2047;
424 Table[2].Attr = 0;
425 #endif
426
427 //Update CRC
428 Head->PartTblCrc = ventoy_crc32(Table, sizeof(gpt->PartTbl));
429 Head->Crc = ventoy_crc32(Head, Head->Length);
430
431 return 0;
432 }
433
434 int VentoyFillMBRLocation(uint64_t DiskSizeInBytes, uint32_t StartSectorId, uint32_t SectorCount, PART_TABLE *Table)
435 {
436 uint8_t Head;
437 uint8_t Sector;
438 uint8_t nSector = 63;
439 uint8_t nHead = 8;
440 uint32_t Cylinder;
441 uint32_t EndSectorId;
442
443 while (nHead != 0 && (DiskSizeInBytes / 512 / nSector / nHead) > 1024)
444 {
445 nHead = (uint8_t)nHead * 2;
446 }
447
448 if (nHead == 0)
449 {
450 nHead = 255;
451 }
452
453 Cylinder = StartSectorId / nSector / nHead;
454 Head = StartSectorId / nSector % nHead;
455 Sector = StartSectorId % nSector + 1;
456
457 Table->StartHead = Head;
458 Table->StartSector = Sector;
459 Table->StartCylinder = Cylinder;
460
461 EndSectorId = StartSectorId + SectorCount - 1;
462 Cylinder = EndSectorId / nSector / nHead;
463 Head = EndSectorId / nSector % nHead;
464 Sector = EndSectorId % nSector + 1;
465
466 Table->EndHead = Head;
467 Table->EndSector = Sector;
468 Table->EndCylinder = Cylinder;
469
470 Table->StartSectorId = StartSectorId;
471 Table->SectorCount = SectorCount;
472
473 return 0;
474 }
475
476 int ventoy_fill_mbr(uint64_t size, uint64_t reserve, int align4k, MBR_HEAD *pMBR)
477 {
478 ventoy_guid Guid;
479 uint32_t DiskSignature;
480 uint32_t DiskSectorCount;
481 uint32_t PartSectorCount;
482 uint32_t PartStartSector;
483 uint32_t ReservedSector;
484
485 VentoyGetLocalBootImg(pMBR);
486
487 ventoy_gen_preudo_uuid(&Guid);
488
489 memcpy(&DiskSignature, &Guid, sizeof(uint32_t));
490
491 vdebug("Disk signature: 0x%08x\n", DiskSignature);
492
493 memcpy(pMBR->BootCode + 0x1B8, &DiskSignature, 4);
494 memcpy(pMBR->BootCode + 0x180, &Guid, 16);
495
496 if (size / 512 > 0xFFFFFFFF)
497 {
498 DiskSectorCount = 0xFFFFFFFF;
499 }
500 else
501 {
502 DiskSectorCount = (uint32_t)(size / 512);
503 }
504
505 if (reserve <= 0)
506 {
507 ReservedSector = 0;
508 }
509 else
510 {
511 ReservedSector = (uint32_t)(reserve / 512);
512 }
513
514 // check aligned with 4KB
515 if (align4k)
516 {
517 uint64_t sectors = size / 512;
518 if (sectors % 8)
519 {
520 vlog("Disk need to align with 4KB %u\n", (uint32_t)(sectors % 8));
521 ReservedSector += (uint32_t)(sectors % 8);
522 }
523 else
524 {
525 vdebug("no need to align with 4KB\n");
526 }
527 }
528
529 vlog("ReservedSector: %u\n", ReservedSector);
530
531 //Part1
532 PartStartSector = VTOYIMG_PART_START_SECTOR;
533 PartSectorCount = DiskSectorCount - ReservedSector - VTOYEFI_PART_BYTES / 512 - PartStartSector;
534 VentoyFillMBRLocation(size, PartStartSector, PartSectorCount, pMBR->PartTbl);
535
536 pMBR->PartTbl[0].Active = 0x80; // bootable
537 pMBR->PartTbl[0].FsFlag = 0x07; // exFAT/NTFS/HPFS
538
539 //Part2
540 PartStartSector += PartSectorCount;
541 PartSectorCount = VTOYEFI_PART_BYTES / 512;
542 VentoyFillMBRLocation(size, PartStartSector, PartSectorCount, pMBR->PartTbl + 1);
543
544 pMBR->PartTbl[1].Active = 0x00;
545 pMBR->PartTbl[1].FsFlag = 0xEF; // EFI System Partition
546
547 pMBR->Byte55 = 0x55;
548 pMBR->ByteAA = 0xAA;
549
550 return 0;
551 }
552