xref: /minix/minix/commands/cdprobe/cdprobe.c (revision 0a6a1f1d)
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.)
5  *
6  * There is some nasty hard-codery in here ( MINIX cd label) that can be
7  * improved on.
8  *
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)
13  */
14 
15 #define CD_SECTOR	2048
16 #define SUPER_OFF	1024
17 #define AT_MINORS	8
18 #define MAGIC_OFF	24
19 
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 int main(void)
28 {
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];
34 
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];
39 
40 			fprintf(stderr, "Trying %s  \r", name);
41 			fflush(stderr);
42 
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;
48 			}
49 
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;
55 			}
56 			r = read(fd, pvd, sizeof(pvd));
57 			close(fd);
58 			if (r != sizeof(pvd)) {
59 				continue;
60 			}
61 
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;
68 			}
69 
70 			fprintf(stderr, "\nFound.\n");
71 			printf("%s\n", name);
72 			return 0;
73 		}
74 	}
75 
76 	fprintf(stderr, "\nNot found.\n");
77 	return 1;
78 }
79