]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Unix/ventoy_unix_src/DragonFly/walk_disk.c
change password input field to type=password (#2427)
[Ventoy.git] / Unix / ventoy_unix_src / DragonFly / walk_disk.c
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Donn Seeley at Berkeley Software Design, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)init.c 8.1 (Berkeley) 7/15/93
34 * $FreeBSD: src/sbin/init/init.c,v 1.38.2.8 2001/10/22 11:27:32 des Exp $
35 */
36
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/ioctl.h>
41 #include <sys/fcntl.h>
42
43 #include <dirent.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fts.h>
47 #include <grp.h>
48 #include <inttypes.h>
49 #include <limits.h>
50 #include <locale.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <vtutil.h>
57
58 static int find_disk_by_signature(uint8_t *uuid, uint8_t *sig, uint64_t size, int *count, char *name)
59 {
60 int fd;
61 int len;
62 int cnt = 0;
63 FTS *ftsp;
64 FTSENT *p;
65 uint8_t mbr[512];
66 char devname[MAXPATHLEN];
67 static char dev[] = "/dev", *devav[] = {dev, NULL};
68
69 vdebug("[VTOY] find_disk_by_signature %llu\n", size);
70
71 ftsp = fts_open(devav, FTS_PHYSICAL | FTS_NOCHDIR, NULL);
72 while ((p = fts_read(ftsp)) != NULL)
73 {
74 if (p->fts_level == 1 && p->fts_statp && p->fts_name && p->fts_statp->st_size == size)
75 {
76 sprintf(devname, "/dev/%s", p->fts_name);
77
78 fd = open(devname, O_RDONLY);
79 if (fd < 0)
80 {
81 continue;
82 }
83
84 memset(mbr, 0, 512);
85 read(fd, mbr, 512);
86 close(fd);
87
88 if (memcmp(mbr + 0x180, uuid, 16) == 0 && memcmp(mbr + 0x1B8, sig, 4) == 0)
89 {
90 cnt++;
91 strcpy(name, p->fts_name);
92 break;
93 }
94 }
95 }
96
97 *count = cnt;
98 fts_close(ftsp);
99
100 return 0;
101 }
102
103 static int find_disk_by_size(uint64_t size, const char *prefix, int *count, char *name)
104 {
105 int len;
106 int cnt = 0;
107 FTS *ftsp;
108 FTSENT *p;
109 static char dev[] = "/dev", *devav[] = {dev, NULL};
110
111 if (prefix)
112 {
113 len = strlen(prefix);
114 }
115
116 name[0] = 0;
117 ftsp = fts_open(devav, FTS_PHYSICAL | FTS_NOCHDIR, NULL);
118 while ((p = fts_read(ftsp)) != NULL)
119 {
120 if (p->fts_level == 1 && p->fts_statp && p->fts_name && p->fts_statp->st_size == size)
121 {
122 if (prefix)
123 {
124 if (strncmp(p->fts_name, prefix, len) == 0)
125 {
126 cnt++;
127 if (name[0] == 0)
128 strcpy(name, p->fts_name);
129 }
130 }
131 else
132 {
133 cnt++;
134 if (name[0] == 0)
135 strcpy(name, p->fts_name);
136 }
137 }
138 }
139
140 *count = cnt;
141 fts_close(ftsp);
142
143 return 0;
144 }
145
146 int prepare_dmtable(void)
147 {
148 int count = 0;
149 uint32_t i = 0;
150 uint32_t sector_start = 0;
151 uint32_t disk_sector_num = 0;
152 FILE *fIn, *fOut;
153 char disk[MAXPATHLEN];
154 char prefix[MAXPATHLEN];
155 ventoy_image_desc desc;
156 ventoy_img_chunk chunk;
157
158 fIn = fopen("/dmtable", "rb");
159 if (!fIn)
160 {
161 printf("Failed to open dmtable\n");
162 return 1;
163 }
164
165 fOut = fopen("/tmp/dmtable", "w+");
166 if (!fOut)
167 {
168 printf("Failed to create /tmp/dmtable %d\n", errno);
169 fclose(fIn);
170 return 1;
171 }
172
173 fread(&desc, 1, sizeof(desc), fIn);
174
175 vdebug("[VTOY] disksize:%lu part1size:%lu chunkcount:%u\n", desc.disk_size, desc.part1_size, desc.img_chunk_count);
176
177 for (i = 0; count <= 0 && i < 10; i++)
178 {
179 sleep(2);
180 find_disk_by_size(desc.part1_size, NULL, &count, disk);
181 vdebug("[VTOY] find disk by part1 size, i=%d, count=%d, %s\n", i, count, disk);
182 }
183
184 if (count == 0)
185 {
186 goto end;
187 }
188 else if (count > 1)
189 {
190 find_disk_by_signature(desc.disk_uuid, desc.disk_signature, desc.disk_size, &count, prefix);
191 vdebug("[VTOY] find disk by signature: %d %s\n", count, prefix);
192
193 if (count != 1)
194 {
195 printf("[VTOY] Failed to find disk by signature\n");
196 goto end;
197 }
198
199 find_disk_by_size(desc.part1_size, prefix, &count, disk);
200 vdebug("[VTOY] find disk by part1 size with prefix %s : %d %s\n", prefix, count, disk);
201 }
202
203 for (i = 0; i < desc.img_chunk_count; i++)
204 {
205 fread(&chunk, 1, sizeof(chunk), fIn);
206
207 sector_start = chunk.img_start_sector;
208 disk_sector_num = (uint32_t)(chunk.disk_end_sector + 1 - chunk.disk_start_sector);
209
210 fprintf(fOut, "%u %u linear /dev/%s %llu\n",
211 (sector_start << 2), disk_sector_num,
212 disk, (unsigned long long)chunk.disk_start_sector - 2048);
213
214 vdebug("%u %u linear /dev/%s %llu\n",
215 (sector_start << 2), disk_sector_num,
216 disk, (unsigned long long)chunk.disk_start_sector - 2048);
217 }
218
219 end:
220 fclose(fIn);
221 fclose(fOut);
222 return 0;
223 }
224