1 /* 2 * Copyright 2003-2005, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "Handle.h" 8 9 #include <boot/platform.h> 10 #include <util/kernel_cpp.h> 11 12 #include <unistd.h> 13 #include <errno.h> 14 #include <dirent.h> 15 #include <limits.h> 16 #include <string.h> 17 #include <stdio.h> 18 19 20 // we're using some libroot specials 21 extern int __libc_argc; 22 extern char **__libc_argv; 23 extern const char *__progname; 24 25 extern bool gShowMenu; 26 27 28 static status_t 29 get_device(const char *path, Node **_device) 30 { 31 Handle *device = new Handle(path); 32 if (device == NULL) 33 return B_NO_MEMORY; 34 35 if (device->InitCheck() != B_OK) { 36 fprintf(stderr, "%s: Could not open image \"%s\": %s\n", 37 __progname, path, strerror(device->InitCheck())); 38 39 delete device; 40 return B_ERROR; 41 } 42 43 *_device = device; 44 return B_OK; 45 } 46 47 48 static status_t 49 add_device(const char *path, NodeList *list) 50 { 51 Node *device; 52 status_t status = get_device(path, &device); 53 if (status < B_OK) 54 return status; 55 56 printf("add \"%s\" to list of boot devices\n", path); 57 list->Add(device); 58 59 return B_OK; 60 } 61 62 63 static status_t 64 recursive_add_device(const char *path, NodeList *list) 65 { 66 DIR *dir = opendir(path); 67 if (dir == NULL) 68 return errno; 69 70 struct dirent *dirent; 71 while ((dirent = readdir(dir)) != NULL) { 72 // we don't care about names with a leading dot (incl. "." and "..") 73 if (dirent->d_name[0] == '.') 74 continue; 75 76 char nextPath[PATH_MAX]; 77 strcpy(nextPath, path); 78 strcat(nextPath, "/"); 79 strcat(nextPath, dirent->d_name); 80 81 // Note, this doesn't care about if it's a directory or not! 82 if (!strcmp(dirent->d_name, "raw") 83 && add_device(nextPath, list) == B_OK) 84 continue; 85 86 recursive_add_device(nextPath, list); 87 } 88 closedir(dir); 89 90 return B_OK; 91 } 92 93 94 static char * 95 get_next_argument(int32 *cookie, bool options) 96 { 97 int32 i = *cookie + 1; 98 99 if (i == 1 && !options) { 100 // filter out options at the start 101 while (i < __libc_argc && __libc_argv[i][0] == '-') 102 i++; 103 } 104 105 for (; i < __libc_argc; i++) { 106 // Options come at the start 107 if (options && __libc_argv[i][0] != '-') 108 return NULL; 109 110 *cookie = i; 111 return __libc_argv[i]; 112 } 113 114 return NULL; 115 } 116 117 118 static char * 119 get_next_option(int32 *cookie) 120 { 121 return get_next_argument(cookie, true); 122 } 123 124 125 static char * 126 get_next_device(int32 *cookie) 127 { 128 return get_next_argument(cookie, false); 129 } 130 131 132 // #pragma mark - 133 134 135 status_t 136 platform_get_boot_device(struct stage2_args *args, Node **_device) 137 { 138 // we accept a boot device from the command line 139 int32 cookie = 0; 140 char *path = get_next_device(&cookie); 141 if (path != NULL) 142 return get_device(path, _device); 143 144 return get_device("/boot/home/test-file-device", _device); 145 } 146 147 148 status_t 149 platform_get_boot_partition(struct stage2_args *args, Node *device, 150 NodeList *list, boot::Partition **_partition) 151 { 152 NodeIterator iterator = list->GetIterator(); 153 boot::Partition *partition = NULL; 154 while ((partition = (boot::Partition *)iterator.Next()) != NULL) { 155 // just take the first partition 156 *_partition = partition; 157 return B_OK; 158 } 159 160 return B_ENTRY_NOT_FOUND; 161 } 162 163 164 status_t 165 platform_add_block_devices(struct stage2_args *args, NodeList *list) 166 { 167 int32 cookie = 0; 168 if (get_next_device(&cookie) != NULL) { 169 // add the devices provided on the command line 170 char *path; 171 while ((path = get_next_device(&cookie)) != NULL) 172 add_device(path, list); 173 } 174 175 bool addDevices = true; 176 bool scsi = true; 177 char *option; 178 cookie = 0; 179 while ((option = get_next_option(&cookie)) != NULL) { 180 if (!strcmp(option, "--no-devices")) 181 addDevices = false; 182 else if (!strcmp(option, "--no-scsi")) 183 scsi = false; 184 else if (!strcmp(option, "--menu")) 185 gShowMenu = true; 186 else { 187 fprintf(stderr, "usage: %s [OPTIONS] [image ...]\n" 188 " --no-devices\tDon't add real devices from /dev/disk\n" 189 " --no-scsi\tDon't add SCSI devices (might be problematic with some\n" 190 "\t\tUSB mass storage drivers)\n" 191 " --menu\tShow boot menu\n", __progname); 192 exit(0); 193 } 194 } 195 196 if (addDevices) { 197 recursive_add_device("/dev/disk/ide", list); 198 recursive_add_device("/dev/disk/virtual", list); 199 200 if (scsi) 201 recursive_add_device("/dev/disk/scsi", list); 202 } 203 204 return B_OK; 205 } 206 207 208 status_t 209 platform_register_boot_device(Node *device) 210 { 211 return B_OK; 212 } 213