]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Unix/ventoy_unix_src/DragonFly/walk_disk.c
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Donn Seeley at Berkeley Software Design, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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 $
37 #include <sys/param.h>
38 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 #include <sys/fcntl.h>
58 static int find_disk_by_signature(uint8_t *uuid
, uint8_t *sig
, uint64_t size
, int *count
, char *name
)
66 char devname
[MAXPATHLEN
];
67 static char dev
[] = "/dev", *devav
[] = {dev
, NULL
};
69 vdebug("[VTOY] find_disk_by_signature %llu\n", size
);
71 ftsp
= fts_open(devav
, FTS_PHYSICAL
| FTS_NOCHDIR
, NULL
);
72 while ((p
= fts_read(ftsp
)) != NULL
)
74 if (p
->fts_level
== 1 && p
->fts_statp
&& p
->fts_name
&& p
->fts_statp
->st_size
== size
)
76 sprintf(devname
, "/dev/%s", p
->fts_name
);
78 fd
= open(devname
, O_RDONLY
);
88 if (memcmp(mbr
+ 0x180, uuid
, 16) == 0 && memcmp(mbr
+ 0x1B8, sig
, 4) == 0)
91 strcpy(name
, p
->fts_name
);
103 static int find_disk_by_size(uint64_t size
, const char *prefix
, int *count
, char *name
)
109 static char dev
[] = "/dev", *devav
[] = {dev
, NULL
};
113 len
= strlen(prefix
);
117 ftsp
= fts_open(devav
, FTS_PHYSICAL
| FTS_NOCHDIR
, NULL
);
118 while ((p
= fts_read(ftsp
)) != NULL
)
120 if (p
->fts_level
== 1 && p
->fts_statp
&& p
->fts_name
&& p
->fts_statp
->st_size
== size
)
124 if (strncmp(p
->fts_name
, prefix
, len
) == 0)
128 strcpy(name
, p
->fts_name
);
135 strcpy(name
, p
->fts_name
);
146 int prepare_dmtable(void)
150 uint32_t sector_start
= 0;
151 uint32_t disk_sector_num
= 0;
153 char disk
[MAXPATHLEN
];
154 char prefix
[MAXPATHLEN
];
155 ventoy_image_desc desc
;
156 ventoy_img_chunk chunk
;
158 fIn
= fopen("/dmtable", "rb");
161 printf("Failed to open dmtable\n");
165 fOut
= fopen("/tmp/dmtable", "w+");
168 printf("Failed to create /tmp/dmtable %d\n", errno
);
173 fread(&desc
, 1, sizeof(desc
), fIn
);
175 vdebug("[VTOY] disksize:%lu part1size:%lu chunkcount:%u\n", desc
.disk_size
, desc
.part1_size
, desc
.img_chunk_count
);
177 for (i
= 0; count
<= 0 && i
< 10; i
++)
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
);
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
);
195 printf("[VTOY] Failed to find disk by signature\n");
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
);
203 for (i
= 0; i
< desc
.img_chunk_count
; i
++)
205 fread(&chunk
, 1, sizeof(chunk
), fIn
);
207 sector_start
= chunk
.img_start_sector
;
208 disk_sector_num
= (uint32_t)(chunk
.disk_end_sector
+ 1 - chunk
.disk_start_sector
);
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);
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);