source: trunk/minix/commands/simple/cdprobe.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.6 KB
RevLine 
[9]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.)
4 *
5 * There is some nasty hard-codery in here ( MINIX cd label) that can be
6 * improved on.
7 *
8 * Changes:
9 * Jul 14, 2005 Created (Ben Gras)
10 * Feb 10, 2006 Changed into a standalone program (Philip Homburg)
11 */
12
13#define CD_SECTOR 2048
14#define SUPER_OFF 1024
15#define AT_MINORS 8
16#define MAGIC_OFF 24
17
18#include <errno.h>
19#include <fcntl.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24
25#include "../../servers/fs/const.h"
26
27char pvd[CD_SECTOR];
28
29/*===========================================================================*
30 * cdprobe *
31 *===========================================================================*/
32int main(void)
33{
34 int i, r, fd, minor, found;
35 off_t pos;
36 u16_t *magicp;
37 char name1[]= "/dev/c0dX";
38 char name2[]= "/dev/c0dXpY";
39
40 found= 0;
41 for(i = 0; i < AT_MINORS; i++) {
42 name1[8]= '0' + i;
43
44 fd = open(name1, O_RDONLY);
45 if (fd < 0)
46 {
47 if (errno != ENXIO)
48 {
49 fprintf(stderr, "open '%s' failed: %s\n",
50 name1, strerror(errno));
51 }
52 continue;
53 }
54
55 pos= lseek(fd, 16*CD_SECTOR, SEEK_SET);
56 if (pos != 16*CD_SECTOR)
57 {
58 /* Strange, do we need to issue a warning? */
59 close(fd);
60 continue;
61 }
62 r = read(fd, pvd, sizeof(pvd));
63 if (r != sizeof(pvd))
64 {
65 fprintf(stderr,
66 "error reading CD label from '%s': %s\n",
67 name1, strerror(errno));
68 close(fd);
69 continue;
70 }
71 close(fd);
72
73 /* Check PVD ID. */
74 if (pvd[0] != 1 || pvd[1] != 'C' || pvd[2] != 'D' ||
75 pvd[3] != '0' || pvd[4] != '0' || pvd[5] != '1' ||
76 pvd[6] != 1 ||
77 strncmp(pvd + 40, "MINIX", 5) != 0) {
78 continue;
79 }
80
81 /* 3. Both c0dXp1 and p2 should have a superblock. */
82 found= 1; /* Assume everything is okay */
83 for (minor = 1; minor <= 2; minor++) {
84 name2[8]= '0' + i;
85 name2[10]= '0' + minor;
86
87 fd = open(name2, O_RDONLY);
88 if (fd < 0)
89 {
90 if (errno != ENXIO)
91 {
92 fprintf(stderr,
93 "open '%s' failed: %s\n",
94 name2, strerror(errno));
95 }
96 found= 0;
97 break;
98 }
99 r = read(fd, pvd, sizeof(pvd));
100 if (r != sizeof(pvd))
101 {
102 fprintf(stderr,
103 "error reading super block from '%s': %s\n",
104 name2, strerror(errno));
105 close(fd);
106 found= 0;
107 break;
108 }
109 close(fd);
110
111 magicp= (u16_t *)&pvd[SUPER_OFF+MAGIC_OFF];
112 if (*magicp != SUPER_V3)
113 {
114 fprintf(stderr, "bad super block on %s\n",
115 name2);
116 found= 0;
117 break;
118 }
119 }
120
121 if (found)
122 {
123 printf("%s\n", name1);
124 exit(0);
125 }
126 }
127
128 return 1;
129}
130
Note: See TracBrowser for help on using the repository browser.