diff options
Diffstat (limited to 'mdk-stage1/tools.c')
| -rw-r--r-- | mdk-stage1/tools.c | 374 |
1 files changed, 52 insertions, 322 deletions
diff --git a/mdk-stage1/tools.c b/mdk-stage1/tools.c index 55db7b158..2e27688ea 100644 --- a/mdk-stage1/tools.c +++ b/mdk-stage1/tools.c @@ -1,7 +1,7 @@ /* - * Guillaume Cottenceau (gc@mandrakesoft.com) + * Guillaume Cottenceau (gc) * - * Copyright 2000 Mandrakesoft + * Copyright 2000 Mandriva * * This software may be freely redistributed under the terms of the GNU * public license. @@ -26,14 +26,12 @@ #include <sys/stat.h> #include <fcntl.h> #include <string.h> -#include <ctype.h> #include <stdio.h> #include <dirent.h> #include <sys/types.h> #include <sys/mount.h> #include <sys/poll.h> #include <errno.h> -#include <sys/utsname.h> #include <sys/ioctl.h> #include <linux/fd.h> #include "stage1.h" @@ -43,202 +41,36 @@ #include "automatic.h" #include "tools.h" +#include "utils.h" +#include "params.h" #include "probing.h" #include "modules.h" #include "lomount.h" -static struct param_elem params[50]; -static int param_number = 0; - -void process_cmdline(void) -{ - char buf[512]; - int size, i; - int fd = -1; - - if (IS_TESTING) { - log_message("TESTING: opening cmdline... "); - - if ((fd = open("cmdline", O_RDONLY)) == -1) - log_message("TESTING: could not open cmdline"); - } - - if (fd == -1) { - log_message("opening /proc/cmdline... "); - - if ((fd = open("/proc/cmdline", O_RDONLY)) == -1) - fatal_error("could not open /proc/cmdline"); - } - - size = read(fd, buf, sizeof(buf)); - buf[size-1] = '\0'; // -1 to eat the \n - close(fd); - - log_message("\t%s", buf); - - i = 0; - while (buf[i] != '\0') { - char *name, *value = NULL; - int j = i; - while (buf[i] != ' ' && buf[i] != '=' && buf[i] != '\0') - i++; - if (i == j) { - i++; - continue; - } - name = memdup(&buf[j], i-j + 1); - name[i-j] = '\0'; - - if (buf[i] == '=') { - int k = i+1; - i++; - while (buf[i] != ' ' && buf[i] != '\0') - i++; - value = memdup(&buf[k], i-k + 1); - value[i-k] = '\0'; - } - - params[param_number].name = name; - params[param_number].value = value; - param_number++; - if (!strcmp(name, "changedisk")) set_param(MODE_CHANGEDISK); - if (!strcmp(name, "updatemodules") || - !strcmp(name, "thirdparty")) set_param(MODE_THIRDPARTY); - if (!strcmp(name, "rescue")) set_param(MODE_RESCUE); - if (!strcmp(name, "keepmounted")) set_param(MODE_KEEP_MOUNTED); - if (!strcmp(name, "noauto")) set_param(MODE_NOAUTO); - if (!strcmp(name, "netauto")) set_param(MODE_NETAUTO); - if (!strcmp(name, "debugstage1")) set_param(MODE_DEBUGSTAGE1); - if (!strcmp(name, "automatic")) { - set_param(MODE_AUTOMATIC); - grab_automatic_params(value); - } - if (buf[i] == '\0') - break; - i++; - } - - if (IS_AUTOMATIC && strcmp(get_auto_value("thirdparty"), "")) { - set_param(MODE_THIRDPARTY); - } - - log_message("\tgot %d args", param_number); -} - - -int stage1_mode = 0; - -int get_param(int i) -{ -#ifdef SPAWN_INTERACTIVE - static int fd = 0; - char buf[5000]; - char * ptr; - int nb; - - if (fd <= 0) { - fd = open(interactive_fifo, O_RDONLY); - if (fd == -1) - return (stage1_mode & i); - fcntl(fd, F_SETFL, O_NONBLOCK); - } - - if (fd > 0) { - if ((nb = read(fd, buf, sizeof(buf))) > 0) { - buf[nb] = '\0'; - ptr = buf; - while ((ptr = strstr(ptr, "+ "))) { - if (!strncmp(ptr+2, "rescue", 6)) set_param(MODE_RESCUE); - ptr++; - } - ptr = buf; - while ((ptr = strstr(ptr, "- "))) { - if (!strncmp(ptr+2, "rescue", 6)) unset_param(MODE_RESCUE); - ptr++; - } - } - } -#endif - - return (stage1_mode & i); -} - -char * get_param_valued(char *param_name) -{ - int i; - for (i = 0; i < param_number ; i++) - if (!strcmp(params[i].name, param_name)) - return params[i].value; - - return NULL; -} - -void set_param_valued(char *param_name, char *param_value) -{ - params[param_number].name = param_name; - params[param_number].value = param_value; - param_number++; -} - -void set_param(int i) -{ - stage1_mode |= i; -} - -void unset_param(int i) -{ - stage1_mode &= ~i; -} - -void unset_automatic(void) -{ - log_message("unsetting automatic"); - unset_param(MODE_AUTOMATIC); - exit_bootsplash(); -} - -// warning, many things rely on the fact that: -// - when failing it returns 0 -// - it stops on first non-digit char -int charstar_to_int(const char * s) +int image_has_stage2() { - int number = 0; - while (*s && isdigit(*s)) { - number = (number * 10) + (*s - '0'); - s++; - } - return number; + return access(COMPRESSED_FILE_REL(IMAGE_LOCATION "/"), R_OK) == 0 || + access(IMAGE_LOCATION "/" LIVE_LOCATION_REL, R_OK) == 0; } -off_t file_size(const char * path) +enum return_type create_IMAGE_LOCATION(char *location_full) { - struct stat statr; - if (stat(path, &statr)) - return -1; - else - return statr.st_size; -} + struct stat statbuf; + int offset = strncmp(location_full, IMAGE_LOCATION_DIR, sizeof(IMAGE_LOCATION_DIR) - 1) == 0 ? sizeof(IMAGE_LOCATION_DIR) - 1 : 0; + char *with_arch = asprintf_("%s/%s", location_full, ARCH); -int total_memory(void) -{ - int value; + log_message("trying %s", with_arch); - /* drakx powered: use /proc/kcore and rounds every 4 Mbytes */ - value = 4 * ((int)((float)file_size("/proc/kcore") / 1024 / 1024 / 4 + 0.5)); - log_message("Total Memory: %d Mbytes", value); + if (stat(with_arch, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) + location_full = with_arch; - return value; -} + log_message("assuming %s is a mirror tree", location_full + offset); + unlink(IMAGE_LOCATION); + if (symlink(location_full + offset, IMAGE_LOCATION) != 0) + return RETURN_ERROR; -int image_has_stage2() -{ -#ifdef MANDRAKE_MOVE - return access(IMAGE_LOCATION "/live_tree.clp", R_OK) == 0; -#else - return access(CLP_FILE_REL(IMAGE_LOCATION "/"), R_OK) == 0 || - access(IMAGE_LOCATION "/" LIVE_LOCATION_REL, R_OK) == 0; -#endif + return RETURN_OK; } int ramdisk_possible(void) @@ -251,12 +83,12 @@ int ramdisk_possible(void) } } -int clp_preload(void) +int compressed_image_preload(void) { if (total_memory() > (IS_RESCUE ? MEM_LIMIT_RESCUE_PRELOAD : MEM_LIMIT_DRAKX_PRELOAD)) return 1; else { - log_message("warning, not preloading clp due to low mem"); + log_message("warning, not preloading compressed due to low mem"); return 0; } } @@ -316,149 +148,72 @@ enum return_type copy_file(char * from, char * to, void (*callback_func)(int ove } } -enum return_type mount_clp(char *clp, char *location_mount) +enum return_type mount_compressed_image(char *compressed_image, char *location_mount) { - if (lomount(clp, location_mount, NULL, 1)) { + if (lomount(compressed_image, location_mount, NULL, 1)) { stg1_error_message("Could not mount compressed loopback :(."); return RETURN_ERROR; } return RETURN_OK; } -enum return_type preload_mount_clp(int clp_fd, int clp_size, char *clp_name, char *location_mount) +enum return_type preload_mount_compressed_fd(int compressed_fd, int image_size, char *image_name, char *location_mount) { int ret; - char *clp_tmpfs = asprintf_("%s/tmp/%s", SLASH_LOCATION, clp_name); -#ifdef MANDRAKE_MOVE - static int count = 0; - char buf[5000]; - sprintf(buf, "Loading program into memory (part %d)...", ++count); -#else + char *compressed_tmpfs = asprintf_("/tmp/%s", image_name); char *buf = "Loading program into memory..."; -#endif - init_progression(buf, clp_size); - ret = save_fd(clp_fd, clp_tmpfs, update_progression); + init_progression(buf, image_size); + ret = save_fd(compressed_fd, compressed_tmpfs, update_progression); end_progression(); if (ret != RETURN_OK) return ret; - return mount_clp(clp_tmpfs, location_mount); + return mount_compressed_image(compressed_tmpfs, location_mount); } -enum return_type mount_clp_may_preload(char *clp_name, char *location_mount, int preload) +enum return_type mount_compressed_image_may_preload(char *image_name, char *location_mount, int preload) { - char *clp = asprintf_("%s/%s", CLP_LOCATION, clp_name); + char *compressed_image = asprintf_("%s/%s", COMPRESSED_LOCATION, image_name); - log_message("mount_clp_may_preload: %s into %s (preload = %d)", clp, location_mount, preload); + log_message("mount_compressed_may_preload: %s into %s (preload = %d)", compressed_image, location_mount, preload); - if (access(clp, R_OK) != 0) return RETURN_ERROR; + if (access(compressed_image, R_OK) != 0) return RETURN_ERROR; if (preload) { - int clp_fd = open(clp, O_RDONLY); - if (clp_fd != -1) { - return preload_mount_clp(clp_fd, file_size(clp), clp_name, location_mount); + int compressed_fd = open(compressed_image, O_RDONLY); + if (compressed_fd != -1) { + return preload_mount_compressed_fd(compressed_fd, file_size(compressed_image), image_name, location_mount); } else { - log_perror(clp); + log_perror(compressed_image); return RETURN_ERROR; } } else { - return mount_clp(clp, location_mount); + return mount_compressed_image(compressed_image, location_mount); } } -#ifndef MANDRAKE_MOVE -enum return_type may_load_clp(void) +enum return_type may_load_compressed_image(void) { if (!IS_RESCUE && access(IMAGE_LOCATION "/" LIVE_LOCATION_REL, R_OK) == 0) { /* LIVE install */ return RETURN_OK; } else { - /* CLP install */ - return mount_clp_may_preload(CLP_NAME(""), STAGE2_LOCATION, clp_preload()); + /* compressed install */ + return mount_compressed_image_may_preload(COMPRESSED_NAME(""), STAGE2_LOCATION, compressed_image_preload()); } } -enum return_type load_clp_fd(int fd, int size) -{ - return preload_mount_clp(fd, size, CLP_NAME(""), STAGE2_LOCATION); -} -#endif - -/* pixel's */ -void * memdup(void *src, size_t size) +enum return_type load_compressed_fd(int fd, int size) { - void * r; - r = malloc(size); - memcpy(r, src, size); - return r; -} - - -void add_to_env(char * name, char * value) -{ - FILE* fakeenv = fopen(SLASH_LOCATION "/tmp/env", "a"); - if (fakeenv) { - char* e = asprintf_("%s=%s\n", name, value); - fwrite(e, 1, strlen(e), fakeenv); - free(e); - fclose(fakeenv); - } else - log_message("couldn't fopen to fake env"); -} - - -char ** list_directory(char * direct) -{ - char * tmp[50000]; /* in /dev there can be many many files.. */ - int i = 0; - struct dirent *ep; - DIR *dp = opendir(direct); - while (dp && (ep = readdir(dp))) { - if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, "..")) { - tmp[i] = strdup(ep->d_name); - i++; - } - } - if (dp) - closedir(dp); - tmp[i] = NULL; - return memdup(tmp, sizeof(char*) * (i+1)); -} - - -int string_array_length(char ** a) -{ - int i = 0; - if (!a) - return -1; - while (a && *a) { - a++; - i++; - } - return i; -} - -int kernel_version(void) -{ - struct utsname val; - if (uname(&val)) { - log_perror("uname failed"); - return -1; - } - return charstar_to_int(val.release + 2); + return preload_mount_compressed_fd(fd, size, COMPRESSED_NAME(""), STAGE2_LOCATION); } int try_mount(char * dev, char * location) { char device_fullname[50]; - strcpy(device_fullname, "/dev/"); - strcat(device_fullname, dev); - - if (my_mount(device_fullname, location, "ext2", 0) == -1 && - my_mount(device_fullname, location, "vfat", 0) == -1 && - my_mount(device_fullname, location, "ntfs", 0) == -1 && - my_mount(device_fullname, location, "reiserfs", 0) == -1 && - my_mount(device_fullname, location, "iso9660", 0) == -1) { + snprintf(device_fullname, sizeof(device_fullname), "/dev/%s", dev); + + if (my_mount(device_fullname, location, "auto", 0) == -1) { return 1; } @@ -471,7 +226,8 @@ int get_disks(char *** names, char *** models) char ** ptr; int count = 0; - my_insmod("sd_mod", ANY_DRIVER_TYPE, NULL, 0); + my_modprobe("ide_disk", ANY_DRIVER_TYPE, NULL); + my_modprobe("sd_mod", ANY_DRIVER_TYPE, NULL); get_medias(DISK, names, models, BUS_ANY); @@ -491,8 +247,8 @@ int get_cdroms(char *** names, char *** models) char ** ptr; int count = 0; - my_insmod("ide-cd", ANY_DRIVER_TYPE, NULL, 0); - my_insmod("sr_mod", ANY_DRIVER_TYPE, NULL, 0); + my_modprobe("ide_cd_mod", ANY_DRIVER_TYPE, NULL); + my_modprobe("sr_mod", ANY_DRIVER_TYPE, NULL); get_medias(CDROM, names, models, BUS_ANY); @@ -510,7 +266,7 @@ char * floppy_device(void) { char ** names, ** models; int fd; - my_insmod("floppy", ANY_DRIVER_TYPE, NULL, 0); + my_modprobe("floppy", ANY_DRIVER_TYPE, NULL); fd = open("/dev/fd0", O_RDONLY|O_NONBLOCK); if (fd != -1) { char drivtyp[17]; @@ -530,36 +286,10 @@ char * floppy_device(void) close(fd); } log_message("seems that you don't have a regular floppy drive"); - my_insmod("sd_mod", ANY_DRIVER_TYPE, NULL, 0); + my_modprobe("sd_mod", ANY_DRIVER_TYPE, NULL); get_medias(FLOPPY, &names, &models, BUS_ANY); if (names && *names) return asprintf_("/dev/%s", *names); else - return "/dev/fd0"; -} - -char * asprintf_(const char *msg, ...) -{ - int n; - char * s; - va_list arg_ptr; - va_start(arg_ptr, msg); - n = vsnprintf(0, 1000000, msg, arg_ptr); - va_start(arg_ptr, msg); - if ((s = malloc(n + 1))) { - vsnprintf(s, n + 1, msg, arg_ptr); - va_end(arg_ptr); - return s; - } - va_end(arg_ptr); - return strdup(""); -} - -int scall_(int retval, char * msg, char * file, int line) -{ - char tmp[5000]; - sprintf(tmp, "%s(%s:%d) failed", msg, file, line); - if (retval) - log_perror(tmp); - return retval; + return NULL; } |
