add opendir alias
[minix.git] / commands / cdprobe / cdprobe.c
blobd3785b82ebdcdf1c59c6f0b26fe964f8817706a9
1 /* This file contains some code to guess where we have to load the
2 * RAM image device from, if started from CD. (In this case it's hard
3 * to tell where this is without diving into BIOS heuristics.)
5 * There is some nasty hard-codery in here ( MINIX cd label) that can be
6 * improved on.
8 * Changes:
9 * Jul 14, 2005 Created (Ben Gras)
10 * Feb 10, 2006 Changed into a standalone program (Philip Homburg)
13 #define CD_SECTOR 2048
14 #define SUPER_OFF 1024
15 #define AT_MINORS 8
16 #define MAGIC_OFF 24
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include "mfs/const.h"
27 char pvd[CD_SECTOR];
29 /*===========================================================================*
30 * cdprobe *
31 *===========================================================================*/
32 int main(void)
34 int controller, disk, r, fd, minor, found;
35 off_t pos;
36 u16_t *magicp;
37 char name1[]= "/dev/c0dX";
38 char name2[]= "/dev/c0dXpY";
39 int probelist[AT_MINORS] = { 2, 3, 1, 0, 6, 7, 5, 4 };
41 found= 0;
42 for(controller = 0; controller <= 1; controller++) {
43 name1[6] = '0' + controller;
44 name2[6] = '0' + controller;
45 for(disk = 0; disk < AT_MINORS; disk++) {
46 name1[8]= '0' + probelist[disk];
48 fprintf(stderr, "Trying %s \r", name1);
49 fflush(stderr);
51 fd = open(name1, O_RDONLY);
52 if (fd < 0)
54 if (errno != ENXIO)
56 fprintf(stderr, "open '%s' failed: %s\n",
57 name1, strerror(errno));
59 continue;
62 pos= lseek(fd, 16*CD_SECTOR, SEEK_SET);
63 if (pos != 16*CD_SECTOR)
65 /* Strange, do we need to issue a warning? */
66 close(fd);
67 continue;
69 r = read(fd, pvd, sizeof(pvd));
70 if (r != sizeof(pvd))
72 fprintf(stderr,
73 "error reading CD label from '%s': %s\n",
74 name1, strerror(errno));
75 close(fd);
76 continue;
78 close(fd);
80 /* Check PVD ID. */
81 if (pvd[0] != 1 || pvd[1] != 'C' || pvd[2] != 'D' ||
82 pvd[3] != '0' || pvd[4] != '0' || pvd[5] != '1' ||
83 pvd[6] != 1 ||
84 strncmp(pvd + 40, "MINIX", 5) != 0) {
85 continue;
88 /* 3. Both cXdYp1 and p2 should have a superblock. */
89 found= 1; /* Assume everything is okay */
90 for (minor = 1; minor <= 2; minor++) {
91 name2[8]= '0' + probelist[disk];
92 name2[10]= '0' + minor;
94 fd = open(name2, O_RDONLY);
95 if (fd < 0)
97 if (errno != ENXIO)
99 fprintf(stderr,
100 "open '%s' failed: %s\n",
101 name2, strerror(errno));
103 found= 0;
104 break;
106 r = read(fd, pvd, sizeof(pvd));
107 if (r != sizeof(pvd))
109 fprintf(stderr,
110 "error reading super block from '%s': %s\n",
111 name2, strerror(errno));
112 close(fd);
113 found= 0;
114 break;
116 close(fd);
118 magicp= (u16_t *)&pvd[SUPER_OFF+MAGIC_OFF];
119 if (*magicp != SUPER_V3)
121 fprintf(stderr, "bad super block on %s\n",
122 name2);
123 found= 0;
124 break;
128 if (found)
130 fprintf(stderr, "\nFound.\n");
131 printf("%s\n", name1);
132 exit(0);
136 fprintf(stderr, "\nNot found.\n");
138 return 1;