1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <alloca.h>
#include <inttypes.h>
#include "do_mounts.h"
#include "kinit.h"
#define BUF_SZ 65536
/* Find dev_t for e.g. "hda,NULL" or "hdb,2" */
static dev_t try_name(char *name, int part)
{
char path[BUF_SZ];
char buf[BUF_SZ];
int range;
unsigned int major_num, minor_num;
dev_t res;
char *s;
int len;
int fd;
/* read device number from /sys/block/.../dev */
snprintf(path, sizeof(path), "/sys/block/%s/dev", name);
fd = open(path, 0, 0);
if (fd < 0)
goto fail;
len = read(fd, buf, BUF_SZ);
close(fd);
if (len <= 0 || len == BUF_SZ || buf[len - 1] != '\n')
goto fail;
buf[len - 1] = '\0';
major_num = strtoul(buf, &s, 10);
if (*s != ':')
goto fail;
minor_num = strtoul(s + 1, &s, 10);
if (*s)
goto fail;
res = makedev(major_num, minor_num);
/* if it's there and we are not looking for a partition - that's it */
if (!part)
return res;
/* otherwise read range from .../range */
snprintf(path, sizeof(path), "/sys/block/%s/range", name);
fd = open(path, 0, 0);
if (fd < 0)
goto fail;
len = read(fd, buf, 32);
close(fd);
if (len <= 0 || len == 32 || buf[len - 1] != '\n')
goto fail;
buf[len - 1] = '\0';
range = strtoul(buf, &s, 10);
if (*s)
goto fail;
/* if partition is within range - we got it */
if (part < range) {
dprintf("kinit: try_name %s,%d = %s\n", name, part,
bdevname(res + part));
return res + part;
}
fail:
return (dev_t) 0;
}
/*
* Convert a name into device number. We accept the following variants:
*
* 1) device number in hexadecimal represents itself
* 2) device number in major:minor decimal represents itself
* 3) /dev/nfs represents Root_NFS
* 4) /dev/<disk_name> represents the device number of disk
* 5) /dev/<disk_name><decimal> represents the device number
* of partition - device number of disk plus the partition number
* 6) /dev/<disk_name>p<decimal> - same as the above, that form is
* used when disk name of partitioned disk ends on a digit.
* 7) an actual block device node in the initramfs filesystem
*
* If name doesn't have fall into the categories above, we return 0.
* Driverfs is used to check if something is a disk name - it has
* all known disks under bus/block/devices. If the disk name
* contains slashes, name of driverfs node has them replaced with
* dots. try_name() does the actual checks, assuming that driverfs
* is mounted on rootfs /sys.
*/
static inline dev_t name_to_dev_t_real(const char *name)
{
char *p;
dev_t res = 0;
char *s;
int part;
struct stat st;
int len;
const char *devname;
char *cptr, *e1, *e2;
int major_num, minor_num;
/* Are we a multi root line? */
if (strchr(name, ','))
return Root_MULTI;
if (name[0] == '/') {
devname = name;
} else {
char *dname = alloca(strlen(name) + 6);
sprintf(dname, "/dev/%s", name);
devname = dname;
}
if (!stat(devname, &st) && S_ISBLK(st.st_mode))
return st.st_rdev;
if (strncmp(name, "/dev/", 5)) {
cptr = strchr(devname+5, ':');
if (cptr && cptr[1] != '\0') {
/* Colon-separated decimal device number */
*cptr = '\0';
major_num = strtoul(devname+5, &e1, 10);
minor_num = strtoul(cptr+1, &e2, 10);
if (!*e1 && !*e2)
return makedev(major_num, minor_num);
*cptr = ':';
} else {
/* Hexadecimal device number */
res = (dev_t) strtoul(name, &p, 16);
if (!*p)
return res;
}
} else {
name += 5;
}
if (!strcmp(name, "nfs"))
return Root_NFS;
if (!strcmp(name, "ram")) /* /dev/ram - historic alias for /dev/ram0 */
return Root_RAM0;
if (!strncmp(name, "mtd", 3))
return Root_MTD;
len = strlen(name);
s = alloca(len + 1);
memcpy(s, name, len + 1);
for (p = s; *p; p++)
if (*p == '/')
*p = '!';
res = try_name(s, 0);
if (res)
return res;
while (p > s && isdigit(p[-1]))
p--;
if (p == s || !*p || *p == '0')
goto fail;
part = strtoul(p, NULL, 10);
*p = '\0';
res = try_name(s, part);
if (res)
return res;
if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
goto fail;
p[-1] = '\0';
res = try_name(s, part);
return res;
fail:
return (dev_t) 0;
}
dev_t name_to_dev_t(const char *name)
{
dev_t dev = name_to_dev_t_real(name);
dprintf("kinit: name_to_dev_t(%s) = %s\n", name, bdevname(dev));
return dev;
}
#ifdef TEST_NAMETODEV /* Standalone test */
int main(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; i++)
name_to_dev_t(argv[i]);
return 0;
}
#endif
|