3 rdev.c - query/set root device.
5 -------------------------------------------------------------------------
7 Date: Sun, 27 Dec 1992 15:55:31 +0000
9 From: almesber@nessie.cs.id.ethz.ch (Werner Almesberger)
10 To: Rik Faith <faith@cs.unc.edu>
12 There are quite a few versions of rdev:
14 - the original rootdev that only printed the current root device, by
16 - rdev that does what rootdev did and that also allows you to change
17 the root (and swap) device, by me.
18 - rdev got renamed to setroot and I think even to rootdev on various
20 - Peter MacDonald added video mode and RAM disk setting and included
21 this version on SLS, called rdev again. I've attached his rdev.c to
24 -------------------------------------------------------------------------
26 Date: 11 Mar 92 21:37:37 GMT
27 Subject: rdev - query/set root device
28 From: almesber@nessie.cs.id.ethz.ch (Werner Almesberger)
29 Organization: Swiss Federal Institute of Technology (ETH), Zurich, CH
31 With all that socket, X11, disk driver and FS hacking going on, apparently
32 nobody has found time to address one of the minor nuisances of life: set-
33 ting the root FS device is still somewhat cumbersome. I've written a little
34 utility which can read and set the root device in boot images:
36 rdev accepts an optional offset argument, just in case the address should
37 ever move from 508. If called without arguments, rdev outputs an mtab line
38 for the current root FS, just like /etc/rootdev does.
40 ramsize sets the size of the ramdisk. If size is zero, no ramdisk is used.
42 vidmode sets the default video mode at bootup time. -1 uses default video
45 -------------------------------------------------------------------------
47 Sun Dec 27 10:42:16 1992: Minor usage changes, faith@cs.unc.edu.
48 Tue Mar 30 09:31:52 1993: rdev -Rn to set root readonly flag, sct@dcs.ed.ac.uk
49 Wed Jun 22 21:12:29 1994: Applied patches from Dave
50 (gentzel@nova.enet.dec.com) to prevent dereferencing
51 the NULL pointer, faith@cs.unc.edu
52 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
53 - added Native Language Support
55 -------------------------------------------------------------------------
67 #include <sys/types.h>
72 /* rdev.c - query/set root device. */
77 puts(_("usage: rdev [ -rv ] [ -o OFFSET ] [ IMAGE [ VALUE [ OFFSET ] ] ]"));
78 puts(_(" rdev /dev/fd0 (or rdev /linux, etc.) displays the current ROOT device"));
79 puts(_(" rdev /dev/fd0 /dev/hda2 sets ROOT to /dev/hda2"));
80 puts(_(" rdev -R /dev/fd0 1 set the ROOTFLAGS (readonly status)"));
81 puts(_(" rdev -r /dev/fd0 627 set the RAMDISK size"));
82 puts(_(" rdev -v /dev/fd0 1 set the bootup VIDEOMODE"));
83 puts(_(" rdev -o N ... use the byte offset N"));
84 puts(_(" rootflags ... same as rdev -R"));
85 puts(_(" ramsize ... same as rdev -r"));
86 puts(_(" vidmode ... same as rdev -v"));
87 puts(_("Note: video modes are: -3=Ask, -2=Extended, -1=NormalVga, 1=key1, 2=key2,..."));
88 puts(_(" use -R 1 to mount root readonly, -R 0 for read/write."));
93 #define DEFAULT_OFFSET 508
102 /* Earlier rdev fails on /dev/ida/c0d0p1 so we allow for
103 recursion in /dev. -- Paul Clements */
104 /* In fact devfs needs deep recursion. */
107 find_dev_recursive(char *dirnamebuf, int number) {
113 if ((dp = opendir(dirnamebuf)) == NULL)
115 dirnamelen = strlen(dirnamebuf);
116 while ((dir = readdir(dp)) != NULL) {
117 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
119 if (dirnamelen + 1 + strlen(dir->d_name) > PATH_MAX)
121 dirnamebuf[dirnamelen] = '/';
122 strcpy(dirnamebuf+dirnamelen+1, dir->d_name);
123 if (lstat(dirnamebuf, &s) < 0)
125 if ((s.st_mode & S_IFMT) == S_IFBLK && s.st_rdev == number)
127 if ((s.st_mode & S_IFMT) == S_IFDIR &&
128 find_dev_recursive(dirnamebuf, number))
131 dirnamebuf[dirnamelen] = 0;
137 find_dev(int number) {
138 static char name[PATH_MAX+1];
141 return "Boot device";
142 strcpy(name, "/dev");
143 if (find_dev_recursive(name, number))
145 sprintf(name, "0x%04x", number);
149 /* The enum values are significant, things are stored in this order,
151 enum { RDEV, VIDMODE, RAMSIZE, __swapdev__, __syssize__, ROOTFLAGS };
152 char *cmdnames[6] = { "rdev", "vidmode", "ramsize", "",
154 char *desc[6] = { "Root device", "Video mode", "Ramsize", "",
156 #define shift(n) argv+=n,argc-=n
159 main(int argc, char **argv) {
160 int image, offset, dev_nr, i, newoffset=-1;
162 unsigned short val, have_val;
166 setlocale(LC_ALL, "");
167 bindtextdomain(PACKAGE, LOCALEDIR);
170 /* use the command name to figure out what we have to do - ugly */
172 if ((ptr = strrchr(argv[0],'/')) != NULL)
176 for (i=0; i<=5; i++) {
177 if (!strcmp(ptr,cmdnames[i])) {
184 if (argv[1][0] != '-')
186 switch (argv[1][1]) {
201 newoffset = atoi(argv[1]+2);
204 } else if (argc > 2) {
205 newoffset = atoi(argv[2]);
209 /* Fall through. . . */
215 /* Here the only sensible way of using rdev */
218 if (stat("/",&s) < 0) die("/");
219 printf("%s /\n", find_dev(s.st_dev));
228 /* Ancient garbage.. */
229 offset = DEFAULT_OFFSET-cmd*2;
233 offset = atoi(argv[3]);
239 if (isdigit(*argv[2])) {
240 /* earlier: specify offset */
241 /* now: specify major,minor */
244 if ((p = strchr(argv[2], ',')) == NULL)
245 die(_("missing comma"));
248 val = ((ma<<8) | mi);
250 char *device = argv[2];
251 if (stat(device,&s) < 0)
262 if ((image = open(argv[1],O_WRONLY)) < 0) die(argv[1]);
263 if (lseek(image,offset,0) < 0) die("lseek");
264 if (write(image,(char *)&val,2) != 2) die(argv[1]);
265 if (close(image) < 0) die("close");
267 if ((image = open(argv[1],O_RDONLY)) < 0) die(argv[1]);
268 if (lseek(image,offset,0) < 0) die("lseek");
270 if (read(image,(char *)&dev_nr,2) != 2) die(argv[1]);
271 if (close(image) < 0) die("close");
272 fputs(desc[cmd], stdout);
274 printf(" %s\n", find_dev(dev_nr));
276 printf(" %d\n", dev_nr);