fe384f774c7759cf8bcc9121b7f78e30e7a55485
[lunaix-os.git] / lunaix-os / kernel / fs / probe_boot.c
1 #include <lunaix/fs/probe_boot.h>
2 #include <lunaix/mm/valloc.h>
3 #include <lunaix/syslog.h>
4
5 #include "iso9660/iso9660.h"
6
7 LOG_MODULE("PROBE")
8
9 #define LUNAIX_ID 0x414e554cUL // "LUNA"
10
11 struct device*
12 probe_boot_medium()
13 {
14     struct device_meta* block_cat = device_getbyname(NULL, "block", 5);
15     if (!block_cat) {
16         return NULL;
17     }
18
19     struct iso_vol_primary* volp = valloc(ISO9660_BLKSZ);
20
21     struct device* dev = NULL;
22     struct device_meta *pos, *n;
23     llist_for_each(pos, n, &block_cat->children, siblings)
24     {
25         dev = resolve_device(pos);
26         if (!dev) {
27             continue;
28         }
29
30         int errno =
31           dev->ops.read(dev, (void*)volp, ISO9660_READ_OFF, ISO9660_BLKSZ);
32         if (errno < 0) {
33             kprintf(KINFO "failed %xh:%xh, /dev/%s",
34                     dev->ident.fn_grp,
35                     dev->ident.unique,
36                     dev->name.value);
37             dev = NULL;
38             goto done;
39         }
40
41         if (*(u32_t*)volp->header.std_id != ISO_SIGNATURE_LO) {
42             continue;
43         }
44
45         if (*(u32_t*)volp->sys_id == LUNAIX_ID) {
46             kprintf(KINFO "%xh:%xh, /dev/%s, %s",
47                     dev->ident.fn_grp,
48                     dev->ident.unique,
49                     dev->name.value,
50                     (char*)volp->vol_id);
51             goto done;
52         }
53     }
54
55     return NULL;
56
57 done:
58     vfree(volp);
59     return dev;
60 }