Magellan Linux

Diff of /tags/grubby-8_32/grubby.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1849 by niro, Mon Jul 2 13:08:29 2012 UTC revision 1853 by niro, Mon Jul 2 13:12:07 2012 UTC
# Line 164  struct keywordTypes grubKeywords[] = { Line 164  struct keywordTypes grubKeywords[] = {
164    
165  const char *grubFindConfig(struct configFileInfo *cfi) {  const char *grubFindConfig(struct configFileInfo *cfi) {
166      static const char *configFiles[] = {      static const char *configFiles[] = {
  "/etc/grub.conf",  
167   "/boot/grub/grub.conf",   "/boot/grub/grub.conf",
168   "/boot/grub/menu.lst",   "/boot/grub/menu.lst",
169     "/etc/grub.conf",
170   NULL   NULL
171      };      };
172      static int i = -1;      static int i = -1;
# Line 2038  void displayEntry(struct singleEntry * e Line 2038  void displayEntry(struct singleEntry * e
2038      }      }
2039  }  }
2040    
2041    int isSuseSystem(void) {
2042        const char * path;
2043        const static char default_path[] = "/etc/SuSE-release";
2044    
2045        if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL)
2046     path = default_path;
2047    
2048        if (!access(path, R_OK))
2049     return 1;
2050        return 0;
2051    }
2052    
2053    int isSuseGrubConf(const char * path) {
2054        FILE * grubConf;
2055        char * line = NULL;
2056        size_t len = 0, res = 0;
2057    
2058        grubConf = fopen(path, "r");
2059        if (!grubConf) {
2060            dbgPrintf("Could not open SuSE configuration file '%s'\n", path);
2061     return 0;
2062        }
2063    
2064        while ((res = getline(&line, &len, grubConf)) != -1) {
2065     if (!strncmp(line, "setup", 5)) {
2066        fclose(grubConf);
2067        free(line);
2068        return 1;
2069     }
2070        }
2071    
2072        dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n",
2073          path);
2074    
2075        fclose(grubConf);
2076        free(line);
2077        return 0;
2078    }
2079    
2080    int suseGrubConfGetLba(const char * path, int * lbaPtr) {
2081        FILE * grubConf;
2082        char * line = NULL;
2083        size_t res = 0, len = 0;
2084    
2085        if (!path) return 1;
2086        if (!lbaPtr) return 1;
2087    
2088        grubConf = fopen(path, "r");
2089        if (!grubConf) return 1;
2090    
2091        while ((res = getline(&line, &len, grubConf)) != -1) {
2092     if (line[res - 1] == '\n')
2093        line[res - 1] = '\0';
2094     else if (len > res)
2095        line[res] = '\0';
2096     else {
2097        line = realloc(line, res + 1);
2098        line[res] = '\0';
2099     }
2100    
2101     if (!strncmp(line, "setup", 5)) {
2102        if (strstr(line, "--force-lba")) {
2103            *lbaPtr = 1;
2104        } else {
2105            *lbaPtr = 0;
2106        }
2107        dbgPrintf("lba: %i\n", *lbaPtr);
2108        break;
2109     }
2110        }
2111    
2112        free(line);
2113        fclose(grubConf);
2114        return 0;
2115    }
2116    
2117    int suseGrubConfGetInstallDevice(const char * path, char ** devicePtr) {
2118        FILE * grubConf;
2119        char * line = NULL;
2120        size_t res = 0, len = 0;
2121        char * lastParamPtr = NULL;
2122        char * secLastParamPtr = NULL;
2123        char installDeviceNumber = '\0';
2124        char * bounds = NULL;
2125    
2126        if (!path) return 1;
2127        if (!devicePtr) return 1;
2128    
2129        grubConf = fopen(path, "r");
2130        if (!grubConf) return 1;
2131    
2132        while ((res = getline(&line, &len, grubConf)) != -1) {
2133     if (strncmp(line, "setup", 5))
2134        continue;
2135    
2136     if (line[res - 1] == '\n')
2137        line[res - 1] = '\0';
2138     else if (len > res)
2139        line[res] = '\0';
2140     else {
2141        line = realloc(line, res + 1);
2142        line[res] = '\0';
2143     }
2144    
2145     lastParamPtr = bounds = line + res;
2146    
2147     /* Last parameter in grub may be an optional IMAGE_DEVICE */
2148     while (!isspace(*lastParamPtr))
2149        lastParamPtr--;
2150     lastParamPtr++;
2151    
2152     secLastParamPtr = lastParamPtr - 2;
2153     dbgPrintf("lastParamPtr: %s\n", lastParamPtr);
2154    
2155     if (lastParamPtr + 3 > bounds) {
2156        dbgPrintf("lastParamPtr going over boundary");
2157        fclose(grubConf);
2158        free(line);
2159        return 1;
2160     }
2161     if (!strncmp(lastParamPtr, "(hd", 3))
2162        lastParamPtr += 3;
2163     dbgPrintf("lastParamPtr: %c\n", *lastParamPtr);
2164    
2165     /*
2166     * Second last parameter will decide wether last parameter is
2167     * an IMAGE_DEVICE or INSTALL_DEVICE
2168     */
2169     while (!isspace(*secLastParamPtr))
2170        secLastParamPtr--;
2171     secLastParamPtr++;
2172    
2173     if (secLastParamPtr + 3 > bounds) {
2174        dbgPrintf("secLastParamPtr going over boundary");
2175        fclose(grubConf);
2176        free(line);
2177        return 1;
2178     }
2179     dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr);
2180     if (!strncmp(secLastParamPtr, "(hd", 3)) {
2181        secLastParamPtr += 3;
2182        dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr);
2183        installDeviceNumber = *secLastParamPtr;
2184     } else {
2185        installDeviceNumber = *lastParamPtr;
2186     }
2187    
2188     *devicePtr = malloc(6);
2189     snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber);
2190     dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber);
2191     fclose(grubConf);
2192     free(line);
2193     return 0;
2194        }
2195    
2196        free(line);
2197        fclose(grubConf);
2198        return 1;
2199    }
2200    
2201    int grubGetBootFromDeviceMap(const char * device,
2202         char ** bootPtr) {
2203        FILE * deviceMap;
2204        char * line = NULL;
2205        size_t res = 0, len = 0;
2206        char * devicePtr;
2207        char * bounds = NULL;
2208        const char * path;
2209        const static char default_path[] = "/boot/grub/device.map";
2210    
2211        if (!device) return 1;
2212        if (!bootPtr) return 1;
2213    
2214        if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL)
2215     path = default_path;
2216    
2217        dbgPrintf("opening grub device.map file from: %s\n", path);
2218        deviceMap = fopen(path, "r");
2219        if (!deviceMap)
2220     return 1;
2221    
2222        while ((res = getline(&line, &len, deviceMap)) != -1) {
2223            if (!strncmp(line, "#", 1))
2224        continue;
2225    
2226     if (line[res - 1] == '\n')
2227        line[res - 1] = '\0';
2228     else if (len > res)
2229        line[res] = '\0';
2230     else {
2231        line = realloc(line, res + 1);
2232        line[res] = '\0';
2233     }
2234    
2235     devicePtr = line;
2236     bounds = line + res;
2237    
2238     while ((isspace(*line) && ((devicePtr + 1) <= bounds)))
2239        devicePtr++;
2240     dbgPrintf("device: %s\n", devicePtr);
2241    
2242     if (!strncmp(devicePtr, device, strlen(device))) {
2243        devicePtr += strlen(device);
2244        while (isspace(*devicePtr) && ((devicePtr + 1) <= bounds))
2245            devicePtr++;
2246    
2247        *bootPtr = strdup(devicePtr);
2248        break;
2249     }
2250        }
2251    
2252        free(line);
2253        fclose(deviceMap);
2254        return 0;
2255    }
2256    
2257    int suseGrubConfGetBoot(const char * path, char ** bootPtr) {
2258        char * grubDevice;
2259    
2260        if (suseGrubConfGetInstallDevice(path, &grubDevice))
2261     dbgPrintf("error looking for grub installation device\n");
2262        else
2263     dbgPrintf("grubby installation device: %s\n", grubDevice);
2264    
2265        if (grubGetBootFromDeviceMap(grubDevice, bootPtr))
2266     dbgPrintf("error looking for grub boot device\n");
2267        else
2268     dbgPrintf("grubby boot device: %s\n", *bootPtr);
2269    
2270        free(grubDevice);
2271        return 0;
2272    }
2273    
2274    int parseSuseGrubConf(int * lbaPtr, char ** bootPtr) {
2275        /*
2276         * This SuSE grub configuration file at this location is not your average
2277         * grub configuration file, but instead the grub commands used to setup
2278         * grub on that system.
2279         */
2280        const char * path;
2281        const static char default_path[] = "/etc/grub.conf";
2282    
2283        if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL)
2284     path = default_path;
2285    
2286        if (!isSuseGrubConf(path)) return 1;
2287    
2288        if (lbaPtr) {
2289            *lbaPtr = 0;
2290            if (suseGrubConfGetLba(path, lbaPtr))
2291                return 1;
2292        }
2293    
2294        if (bootPtr) {
2295            *bootPtr = NULL;
2296            suseGrubConfGetBoot(path, bootPtr);
2297        }
2298    
2299        return 0;
2300    }
2301    
2302  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
2303      FILE * in;      FILE * in;
2304      char buf[1024];      char buf[1024];
# Line 2086  int parseSysconfigGrub(int * lbaPtr, cha Line 2347  int parseSysconfigGrub(int * lbaPtr, cha
2347  }  }
2348    
2349  void dumpSysconfigGrub(void) {  void dumpSysconfigGrub(void) {
2350      char * boot;      char * boot = NULL;
2351      int lba;      int lba;
2352    
2353      if (!parseSysconfigGrub(&lba, &boot)) {      if (isSuseSystem()) {
2354   if (lba) printf("lba\n");          if (parseSuseGrubConf(&lba, &boot)) {
2355   if (boot) printf("boot=%s\n", boot);      free(boot);
2356        return;
2357     }
2358        } else {
2359            if (parseSysconfigGrub(&lba, &boot)) {
2360        free(boot);
2361        return;
2362     }
2363        }
2364    
2365        if (lba) printf("lba\n");
2366        if (boot) {
2367     printf("boot=%s\n", boot);
2368     free(boot);
2369      }      }
2370  }  }
2371    
# Line 2905  int checkForGrub(struct grubConfig * con Line 3179  int checkForGrub(struct grubConfig * con
3179      int fd;      int fd;
3180      unsigned char bootSect[512];      unsigned char bootSect[512];
3181      char * boot;      char * boot;
3182        int onSuse = isSuseSystem();
3183    
3184      if (parseSysconfigGrub(NULL, &boot))  
3185   return 0;      if (onSuse) {
3186     if (parseSuseGrubConf(NULL, &boot))
3187        return 0;
3188        } else {
3189     if (parseSysconfigGrub(NULL, &boot))
3190        return 0;
3191        }
3192    
3193      /* assume grub is not installed -- not an error condition */      /* assume grub is not installed -- not an error condition */
3194      if (!boot)      if (!boot)
# Line 2926  int checkForGrub(struct grubConfig * con Line 3207  int checkForGrub(struct grubConfig * con
3207      }      }
3208      close(fd);      close(fd);
3209    
3210        /* The more elaborate checks do not work on SuSE. The checks done
3211         * seem to be reasonble (at least for now), so just return success
3212         */
3213        if (onSuse)
3214     return 2;
3215    
3216      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3217  }  }
3218    
# Line 2959  int checkForExtLinux(struct grubConfig * Line 3246  int checkForExtLinux(struct grubConfig *
3246      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3247  }  }
3248    
3249    int checkForYaboot(struct grubConfig * config) {
3250        /*
3251         * This is a simplistic check that we consider good enough for own puporses
3252         *
3253         * If we were to properly check if yaboot is *installed* we'd need to:
3254         * 1) get the system boot device (LT_BOOT)
3255         * 2) considering it's a raw filesystem, check if the yaboot binary matches
3256         *    the content on the boot device
3257         * 3) if not, copy the binary to a temporary file and run "addnote" on it
3258         * 4) check again if binary and boot device contents match
3259         */
3260        if (!access("/etc/yaboot.conf", R_OK))
3261     return 2;
3262    
3263        return 1;
3264    }
3265    
3266  static char * getRootSpecifier(char * str) {  static char * getRootSpecifier(char * str) {
3267      char * idx, * rootspec = NULL;      char * idx, * rootspec = NULL;
3268    
# Line 3446  int main(int argc, const char ** argv) { Line 3750  int main(int argc, const char ** argv) {
3750   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
3751      _("filestystem which contains /boot directory (for testing only)"),      _("filestystem which contains /boot directory (for testing only)"),
3752      _("bootfs") },      _("bootfs") },
3753  #if defined(__i386__) || defined(__x86_64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__)
3754   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
3755      _("check if lilo is installed on lilo.conf boot sector") },      _("check which bootloader is installed on boot sector") },
3756  #endif  #endif
3757   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,
3758      _("path to grub config file to update (\"-\" for stdin)"),      _("path to grub config file to update (\"-\" for stdin)"),
# Line 3688  int main(int argc, const char ** argv) { Line 3992  int main(int argc, const char ** argv) {
3992      }      }
3993    
3994      if (bootloaderProbe) {      if (bootloaderProbe) {
3995   int lrc = 0, grc = 0, gr2c = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, erc = 0, yrc = 0;
3996   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig, * yconfig;
3997    
3998   const char *grub2config = grub2FindConfig(&grub2ConfigType);   const char *grub2config = grub2FindConfig(&grub2ConfigType);
3999   if (grub2config) {   if (grub2config) {
# Line 3725  int main(int argc, const char ** argv) { Line 4029  int main(int argc, const char ** argv) {
4029   erc = checkForExtLinux(lconfig);   erc = checkForExtLinux(lconfig);
4030   }   }
4031    
4032   if (lrc == 1 || grc == 1 || gr2c == 1) return 1;  
4033     if (!access(yabootConfigType.defaultConfig, F_OK)) {
4034        yconfig = readConfig(yabootConfigType.defaultConfig,
4035     &yabootConfigType);
4036        if (!yconfig)
4037     yrc = 1;
4038        else
4039          yrc = checkForYaboot(lconfig);
4040     }
4041    
4042     if (lrc == 1 || grc == 1 || gr2c == 1 || yrc == 1) return 1;
4043    
4044   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
4045   if (gr2c == 2) printf("grub2\n");   if (gr2c == 2) printf("grub2\n");
4046   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
4047   if (erc == 2) printf("extlinux\n");   if (erc == 2) printf("extlinux\n");
4048     if (yrc == 2) printf("yaboot\n");
4049    
4050   return 0;   return 0;
4051      }      }

Legend:
Removed from v.1849  
changed lines
  Added in v.1853