Improve the process for GNU tools
[minix3.git] / minix / commands / cdprobe / cdprobe.c
blob0d1319ecc4b90fedcae05ffab5b77e962d1b01d2
1 /*
2 * This file contains some code to guess where we have to load the
3 * RAM image device from, if started from CD. (In this case it's hard
4 * to tell where this is without diving into BIOS heuristics.)
6 * There is some nasty hard-codery in here ( MINIX cd label) that can be
7 * improved on.
9 * Changes:
10 * Jul 14, 2005 Created (Ben Gras)
11 * Feb 10, 2006 Changed into a standalone program (Philip Homburg)
12 * May 25, 2015 Installation CD overhaul (Jean-Baptiste Boric)
15 #define CD_SECTOR 2048
16 #define SUPER_OFF 1024
17 #define AT_MINORS 8
18 #define MAGIC_OFF 24
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 int main(void)
29 const int probelist[AT_MINORS] = { 2, 3, 1, 0, 6, 7, 5, 4 };
30 int controller, disk, r, fd;
31 off_t pos;
32 char name[] = "/dev/c0dX";
33 char pvd[CD_SECTOR];
35 for(controller = 0; controller <= 1; controller++) {
36 name[6] = '0' + controller;
37 for(disk = 0; disk < AT_MINORS; disk++) {
38 name[8]= '0' + probelist[disk];
40 fprintf(stderr, "Trying %s \r", name);
41 fflush(stderr);
43 fd = open(name, O_RDONLY);
44 if ((fd < 0) && (errno != ENXIO)) {
45 fprintf(stderr, "open '%s' failed: %s\n",
46 name, strerror(errno));
47 continue;
50 /* Try to read PVD. */
51 pos = lseek(fd, 16*CD_SECTOR, SEEK_SET);
52 if (pos != 16*CD_SECTOR) {
53 close(fd);
54 continue;
56 r = read(fd, pvd, sizeof(pvd));
57 close(fd);
58 if (r != sizeof(pvd)) {
59 continue;
62 /* Check PVD ID. */
63 if (pvd[0] != 1 || pvd[1] != 'C' || pvd[2] != 'D' ||
64 pvd[3] != '0' || pvd[4] != '0' || pvd[5] != '1' ||
65 pvd[6] != 1 ||
66 strncmp(pvd + 40, "MINIX", 5) != 0) {
67 continue;
70 fprintf(stderr, "\nFound.\n");
71 printf("%s\n", name);
72 return 0;
76 fprintf(stderr, "\nNot found.\n");
77 return 1;