Magellan Linux

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

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

revision 1844 by niro, Mon Jul 2 12:59:07 2012 UTC revision 1934 by niro, Mon Oct 1 12:08:46 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 205  struct keywordTypes grub2Keywords[] = { Line 205  struct keywordTypes grub2Keywords[] = {
205      { "default",    LT_DEFAULT,     ' ' },      { "default",    LT_DEFAULT,     ' ' },
206      { "fallback",   LT_FALLBACK,    ' ' },      { "fallback",   LT_FALLBACK,    ' ' },
207      { "linux",      LT_KERNEL,      ' ' },      { "linux",      LT_KERNEL,      ' ' },
208        { "linuxefi",   LT_KERNEL,      ' ' },
209      { "initrd",     LT_INITRD,      ' ', ' ' },      { "initrd",     LT_INITRD,      ' ', ' ' },
210        { "initrdefi",  LT_INITRD,      ' ', ' ' },
211      { "module",     LT_MBMODULE,    ' ' },      { "module",     LT_MBMODULE,    ' ' },
212      { "kernel",     LT_HYPER,       ' ' },      { "kernel",     LT_HYPER,       ' ' },
213      { NULL, 0, 0 },      { NULL, 0, 0 },
# Line 243  const char *grub2FindConfig(struct confi Line 245  const char *grub2FindConfig(struct confi
245  }  }
246    
247  int sizeOfSingleLine(struct singleLine * line) {  int sizeOfSingleLine(struct singleLine * line) {
   int i;  
248    int count = 0;    int count = 0;
249    
250    for (i = 0; i < line->numElements; i++) {    for (int i = 0; i < line->numElements; i++) {
251      int indentSize = 0;      int indentSize = 0;
252    
253      count = count + strlen(line->elements[i].item);      count = count + strlen(line->elements[i].item);
# Line 573  static char * sdupprintf(const char *for Line 574  static char * sdupprintf(const char *for
574    
575  static struct keywordTypes * getKeywordByType(enum lineType_e type,  static struct keywordTypes * getKeywordByType(enum lineType_e type,
576        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
577      struct keywordTypes * kw;      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
     for (kw = cfi->keywords; kw->key; kw++) {  
578   if (kw->type == type)   if (kw->type == type)
579      return kw;      return kw;
580      }      }
# Line 604  static char * getuuidbydev(char *device) Line 604  static char * getuuidbydev(char *device)
604    
605  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char * keyword,
606   struct configFileInfo * cfi) {   struct configFileInfo * cfi) {
607      struct keywordTypes * kw;      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
     for (kw = cfi->keywords; kw->key; kw++) {  
608   if (!strcmp(keyword, kw->key))   if (!strcmp(keyword, kw->key))
609      return kw->type;      return kw->type;
610      }      }
# Line 692  static void lineInit(struct singleLine * Line 691  static void lineInit(struct singleLine *
691  }  }
692    
693  struct singleLine * lineDup(struct singleLine * line) {  struct singleLine * lineDup(struct singleLine * line) {
     int i;  
694      struct singleLine * newLine = malloc(sizeof(*newLine));      struct singleLine * newLine = malloc(sizeof(*newLine));
695    
696      newLine->indent = strdup(line->indent);      newLine->indent = strdup(line->indent);
# Line 702  struct singleLine * lineDup(struct singl Line 700  struct singleLine * lineDup(struct singl
700      newLine->elements = malloc(sizeof(*newLine->elements) *      newLine->elements = malloc(sizeof(*newLine->elements) *
701         newLine->numElements);         newLine->numElements);
702    
703      for (i = 0; i < newLine->numElements; i++) {      for (int i = 0; i < newLine->numElements; i++) {
704   newLine->elements[i].indent = strdup(line->elements[i].indent);   newLine->elements[i].indent = strdup(line->elements[i].indent);
705   newLine->elements[i].item = strdup(line->elements[i].item);   newLine->elements[i].item = strdup(line->elements[i].item);
706      }      }
# Line 711  struct singleLine * lineDup(struct singl Line 709  struct singleLine * lineDup(struct singl
709  }  }
710    
711  static void lineFree(struct singleLine * line) {  static void lineFree(struct singleLine * line) {
     int i;  
   
712      if (line->indent) free(line->indent);      if (line->indent) free(line->indent);
713    
714      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
715   free(line->elements[i].item);   free(line->elements[i].item);
716   free(line->elements[i].indent);   free(line->elements[i].indent);
717      }      }
# Line 726  static void lineFree(struct singleLine * Line 722  static void lineFree(struct singleLine *
722    
723  static int lineWrite(FILE * out, struct singleLine * line,  static int lineWrite(FILE * out, struct singleLine * line,
724       struct configFileInfo * cfi) {       struct configFileInfo * cfi) {
     int i;  
   
725      if (fprintf(out, "%s", line->indent) == -1) return -1;      if (fprintf(out, "%s", line->indent) == -1) return -1;
726    
727      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
728   /* Need to handle this, because we strip the quotes from   /* Need to handle this, because we strip the quotes from
729   * menuentry when read it. */   * menuentry when read it. */
730   if (line->type == LT_MENUENTRY && i == 1) {   if (line->type == LT_MENUENTRY && i == 1) {
# Line 836  static int getNextLine(char ** bufPtr, s Line 830  static int getNextLine(char ** bufPtr, s
830      if (*line->elements[0].item == '#') {      if (*line->elements[0].item == '#') {
831   char * fullLine;   char * fullLine;
832   int len;   int len;
  int i;  
833    
834   len = strlen(line->indent);   len = strlen(line->indent);
835   for (i = 0; i < line->numElements; i++)   for (int i = 0; i < line->numElements; i++)
836      len += strlen(line->elements[i].item) +      len += strlen(line->elements[i].item) +
837     strlen(line->elements[i].indent);     strlen(line->elements[i].indent);
838    
# Line 848  static int getNextLine(char ** bufPtr, s Line 841  static int getNextLine(char ** bufPtr, s
841   free(line->indent);   free(line->indent);
842   line->indent = fullLine;   line->indent = fullLine;
843    
844   for (i = 0; i < line->numElements; i++) {   for (int i = 0; i < line->numElements; i++) {
845      strcat(fullLine, line->elements[i].item);      strcat(fullLine, line->elements[i].item);
846      strcat(fullLine, line->elements[i].indent);      strcat(fullLine, line->elements[i].indent);
847      free(line->elements[i].item);      free(line->elements[i].item);
# Line 867  static int getNextLine(char ** bufPtr, s Line 860  static int getNextLine(char ** bufPtr, s
860   * elements up more   * elements up more
861   */   */
862   if (!isspace(kw->separatorChar)) {   if (!isspace(kw->separatorChar)) {
     int i;  
863      char indent[2] = "";      char indent[2] = "";
864      indent[0] = kw->separatorChar;      indent[0] = kw->separatorChar;
865      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
866   char *p;   char *p;
  int j;  
867   int numNewElements;   int numNewElements;
868    
869   numNewElements = 0;   numNewElements = 0;
# Line 888  static int getNextLine(char ** bufPtr, s Line 879  static int getNextLine(char ** bufPtr, s
879      sizeof(*line->elements) * elementsAlloced);      sizeof(*line->elements) * elementsAlloced);
880   }   }
881    
882   for (j = line->numElements; j > i; j--) {   for (int j = line->numElements; j > i; j--) {
883   line->elements[j + numNewElements] = line->elements[j];   line->elements[j + numNewElements] = line->elements[j];
884   }   }
885   line->numElements += numNewElements;   line->numElements += numNewElements;
# Line 926  static struct grubConfig * readConfig(co Line 917  static struct grubConfig * readConfig(co
917      struct singleLine * last = NULL, * line, * defaultLine = NULL;      struct singleLine * last = NULL, * line, * defaultLine = NULL;
918      char * end;      char * end;
919      struct singleEntry * entry = NULL;      struct singleEntry * entry = NULL;
920      int i, len;      int len;
921      char * buf;      char * buf;
922    
923      if (!strcmp(inName, "-")) {      if (!strcmp(inName, "-")) {
# Line 989  static struct grubConfig * readConfig(co Line 980  static struct grubConfig * readConfig(co
980   }   }
981    
982   if (line->type == LT_SET_VARIABLE) {   if (line->type == LT_SET_VARIABLE) {
     int i;  
983      dbgPrintf("found 'set' command (%d elements): ", line->numElements);      dbgPrintf("found 'set' command (%d elements): ", line->numElements);
984      dbgPrintf("%s", line->indent);      dbgPrintf("%s", line->indent);
985      for (i = 0; i < line->numElements; i++)      for (int i = 0; i < line->numElements; i++)
986   dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent);   dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent);
987      dbgPrintf("\n");      dbgPrintf("\n");
988      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
# Line 1020  static struct grubConfig * readConfig(co Line 1010  static struct grubConfig * readConfig(co
1010       * This only applies to grub, but that's the only place we       * This only applies to grub, but that's the only place we
1011       * should find LT_MBMODULE lines anyway.       * should find LT_MBMODULE lines anyway.
1012       */       */
1013      struct singleLine * l;      for (struct singleLine *l = entry->lines; l; l = l->next) {
     for (l = entry->lines; l; l = l->next) {  
1014   if (l->type == LT_HYPER)   if (l->type == LT_HYPER)
1015      break;      break;
1016   else if (l->type == LT_KERNEL) {   else if (l->type == LT_KERNEL) {
# Line 1041  static struct grubConfig * readConfig(co Line 1030  static struct grubConfig * readConfig(co
1030   } else if (line->type == LT_TITLE && line->numElements > 1) {   } else if (line->type == LT_TITLE && line->numElements > 1) {
1031      /* make the title a single argument (undoing our parsing) */      /* make the title a single argument (undoing our parsing) */
1032      len = 0;      len = 0;
1033      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1034   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
1035   len += strlen(line->elements[i].indent);   len += strlen(line->elements[i].indent);
1036      }      }
1037      buf = malloc(len + 1);      buf = malloc(len + 1);
1038      *buf = '\0';      *buf = '\0';
1039    
1040      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1041   strcat(buf, line->elements[i].item);   strcat(buf, line->elements[i].item);
1042   free(line->elements[i].item);   free(line->elements[i].item);
1043    
# Line 1068  static struct grubConfig * readConfig(co Line 1057  static struct grubConfig * readConfig(co
1057      char *extras;      char *extras;
1058      char *title;      char *title;
1059    
1060      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1061   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
1062   len += strlen(line->elements[i].indent);   len += strlen(line->elements[i].indent);
1063      }      }
# Line 1080  static struct grubConfig * readConfig(co Line 1069  static struct grubConfig * readConfig(co
1069      *extras = '\0';      *extras = '\0';
1070    
1071      /* get title. */      /* get title. */
1072      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
1073   if (!strcmp(line->elements[i].item, "menuentry"))   if (!strcmp(line->elements[i].item, "menuentry"))
1074      continue;      continue;
1075   if (isquote(*line->elements[i].item))   if (isquote(*line->elements[i].item))
# Line 1100  static struct grubConfig * readConfig(co Line 1089  static struct grubConfig * readConfig(co
1089    
1090      /* get extras */      /* get extras */
1091      int count = 0;      int count = 0;
1092      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
1093   if (count >= 2) {   if (count >= 2) {
1094      strcat(extras, line->elements[i].item);      strcat(extras, line->elements[i].item);
1095      strcat(extras, line->elements[i].indent);      strcat(extras, line->elements[i].indent);
# Line 1221  static struct grubConfig * readConfig(co Line 1210  static struct grubConfig * readConfig(co
1210      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);
1211      if (*end) cfg->defaultImage = -1;      if (*end) cfg->defaultImage = -1;
1212   } else if (defaultLine->numElements >= 2) {   } else if (defaultLine->numElements >= 2) {
1213      i = 0;      int i = 0;
1214      while ((entry = findEntryByIndex(cfg, i))) {      while ((entry = findEntryByIndex(cfg, i))) {
1215   for (line = entry->lines; line; line = line->next)   for (line = entry->lines; line; line = line->next)
1216      if (line->type == LT_TITLE) break;      if (line->type == LT_TITLE) break;
# Line 1323  static int writeConfig(struct grubConfig Line 1312  static int writeConfig(struct grubConfig
1312    
1313      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1314         directory to the dir of the symlink */         directory to the dir of the symlink */
1315              rc = chdir(dirname(strdupa(outName)));      char *dir = strdupa(outName);
1316        rc = chdir(dirname(dir));
1317      do {      do {
1318   buf = alloca(len + 1);   buf = alloca(len + 1);
1319   rc = readlink(basename(outName), buf, len);   rc = readlink(basename(outName), buf, len);
# Line 1880  void markRemovedImage(struct grubConfig Line 1870  void markRemovedImage(struct grubConfig
1870    
1871  void setDefaultImage(struct grubConfig * config, int hasNew,  void setDefaultImage(struct grubConfig * config, int hasNew,
1872       const char * defaultKernelPath, int newIsDefault,       const char * defaultKernelPath, int newIsDefault,
1873       const char * prefix, int flags) {       const char * prefix, int flags, int index) {
1874      struct singleEntry * entry, * entry2, * newDefault;      struct singleEntry * entry, * entry2, * newDefault;
1875      int i, j;      int i, j;
1876    
1877      if (newIsDefault) {      if (newIsDefault) {
1878   config->defaultImage = 0;   config->defaultImage = 0;
1879   return;   return;
1880        } else if ((index >= 0) && config->cfi->defaultIsIndex) {
1881     if (findEntryByIndex(config, index))
1882        config->defaultImage = index;
1883     else
1884        config->defaultImage = -1;
1885     return;
1886      } else if (defaultKernelPath) {      } else if (defaultKernelPath) {
1887   i = 0;   i = 0;
1888   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {   if (findEntryByPath(config, defaultKernelPath, prefix, &i)) {
# Line 1967  void displayEntry(struct singleEntry * e Line 1963  void displayEntry(struct singleEntry * e
1963          return;          return;
1964      }      }
1965    
1966      printf("kernel=%s%s\n", prefix, line->elements[1].item);      if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
1967     printf("kernel=%s\n", line->elements[1].item);
1968        else
1969     printf("kernel=%s%s\n", prefix, line->elements[1].item);
1970    
1971      if (line->numElements >= 3) {      if (line->numElements >= 3) {
1972   printf("args=\"");   printf("args=\"");
# Line 2026  void displayEntry(struct singleEntry * e Line 2025  void displayEntry(struct singleEntry * e
2025      line = getLineByType(LT_INITRD, entry->lines);      line = getLineByType(LT_INITRD, entry->lines);
2026    
2027      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2028   printf("initrd=%s", prefix);   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2029        printf("initrd=");
2030     else
2031        printf("initrd=%s", prefix);
2032    
2033   for (i = 1; i < line->numElements; i++)   for (i = 1; i < line->numElements; i++)
2034      printf("%s%s", line->elements[i].item, line->elements[i].indent);      printf("%s%s", line->elements[i].item, line->elements[i].indent);
2035   printf("\n");   printf("\n");
# Line 2044  void displayEntry(struct singleEntry * e Line 2047  void displayEntry(struct singleEntry * e
2047      }      }
2048  }  }
2049    
2050    int isSuseSystem(void) {
2051        const char * path;
2052        const static char default_path[] = "/etc/SuSE-release";
2053    
2054        if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL)
2055     path = default_path;
2056    
2057        if (!access(path, R_OK))
2058     return 1;
2059        return 0;
2060    }
2061    
2062    int isSuseGrubConf(const char * path) {
2063        FILE * grubConf;
2064        char * line = NULL;
2065        size_t len = 0, res = 0;
2066    
2067        grubConf = fopen(path, "r");
2068        if (!grubConf) {
2069            dbgPrintf("Could not open SuSE configuration file '%s'\n", path);
2070     return 0;
2071        }
2072    
2073        while ((res = getline(&line, &len, grubConf)) != -1) {
2074     if (!strncmp(line, "setup", 5)) {
2075        fclose(grubConf);
2076        free(line);
2077        return 1;
2078     }
2079        }
2080    
2081        dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n",
2082          path);
2083    
2084        fclose(grubConf);
2085        free(line);
2086        return 0;
2087    }
2088    
2089    int suseGrubConfGetLba(const char * path, int * lbaPtr) {
2090        FILE * grubConf;
2091        char * line = NULL;
2092        size_t res = 0, len = 0;
2093    
2094        if (!path) return 1;
2095        if (!lbaPtr) return 1;
2096    
2097        grubConf = fopen(path, "r");
2098        if (!grubConf) return 1;
2099    
2100        while ((res = getline(&line, &len, grubConf)) != -1) {
2101     if (line[res - 1] == '\n')
2102        line[res - 1] = '\0';
2103     else if (len > res)
2104        line[res] = '\0';
2105     else {
2106        line = realloc(line, res + 1);
2107        line[res] = '\0';
2108     }
2109    
2110     if (!strncmp(line, "setup", 5)) {
2111        if (strstr(line, "--force-lba")) {
2112            *lbaPtr = 1;
2113        } else {
2114            *lbaPtr = 0;
2115        }
2116        dbgPrintf("lba: %i\n", *lbaPtr);
2117        break;
2118     }
2119        }
2120    
2121        free(line);
2122        fclose(grubConf);
2123        return 0;
2124    }
2125    
2126    int suseGrubConfGetInstallDevice(const char * path, char ** devicePtr) {
2127        FILE * grubConf;
2128        char * line = NULL;
2129        size_t res = 0, len = 0;
2130        char * lastParamPtr = NULL;
2131        char * secLastParamPtr = NULL;
2132        char installDeviceNumber = '\0';
2133        char * bounds = NULL;
2134    
2135        if (!path) return 1;
2136        if (!devicePtr) return 1;
2137    
2138        grubConf = fopen(path, "r");
2139        if (!grubConf) return 1;
2140    
2141        while ((res = getline(&line, &len, grubConf)) != -1) {
2142     if (strncmp(line, "setup", 5))
2143        continue;
2144    
2145     if (line[res - 1] == '\n')
2146        line[res - 1] = '\0';
2147     else if (len > res)
2148        line[res] = '\0';
2149     else {
2150        line = realloc(line, res + 1);
2151        line[res] = '\0';
2152     }
2153    
2154     lastParamPtr = bounds = line + res;
2155    
2156     /* Last parameter in grub may be an optional IMAGE_DEVICE */
2157     while (!isspace(*lastParamPtr))
2158        lastParamPtr--;
2159     lastParamPtr++;
2160    
2161     secLastParamPtr = lastParamPtr - 2;
2162     dbgPrintf("lastParamPtr: %s\n", lastParamPtr);
2163    
2164     if (lastParamPtr + 3 > bounds) {
2165        dbgPrintf("lastParamPtr going over boundary");
2166        fclose(grubConf);
2167        free(line);
2168        return 1;
2169     }
2170     if (!strncmp(lastParamPtr, "(hd", 3))
2171        lastParamPtr += 3;
2172     dbgPrintf("lastParamPtr: %c\n", *lastParamPtr);
2173    
2174     /*
2175     * Second last parameter will decide wether last parameter is
2176     * an IMAGE_DEVICE or INSTALL_DEVICE
2177     */
2178     while (!isspace(*secLastParamPtr))
2179        secLastParamPtr--;
2180     secLastParamPtr++;
2181    
2182     if (secLastParamPtr + 3 > bounds) {
2183        dbgPrintf("secLastParamPtr going over boundary");
2184        fclose(grubConf);
2185        free(line);
2186        return 1;
2187     }
2188     dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr);
2189     if (!strncmp(secLastParamPtr, "(hd", 3)) {
2190        secLastParamPtr += 3;
2191        dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr);
2192        installDeviceNumber = *secLastParamPtr;
2193     } else {
2194        installDeviceNumber = *lastParamPtr;
2195     }
2196    
2197     *devicePtr = malloc(6);
2198     snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber);
2199     dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber);
2200     fclose(grubConf);
2201     free(line);
2202     return 0;
2203        }
2204    
2205        free(line);
2206        fclose(grubConf);
2207        return 1;
2208    }
2209    
2210    int grubGetBootFromDeviceMap(const char * device,
2211         char ** bootPtr) {
2212        FILE * deviceMap;
2213        char * line = NULL;
2214        size_t res = 0, len = 0;
2215        char * devicePtr;
2216        char * bounds = NULL;
2217        const char * path;
2218        const static char default_path[] = "/boot/grub/device.map";
2219    
2220        if (!device) return 1;
2221        if (!bootPtr) return 1;
2222    
2223        if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL)
2224     path = default_path;
2225    
2226        dbgPrintf("opening grub device.map file from: %s\n", path);
2227        deviceMap = fopen(path, "r");
2228        if (!deviceMap)
2229     return 1;
2230    
2231        while ((res = getline(&line, &len, deviceMap)) != -1) {
2232            if (!strncmp(line, "#", 1))
2233        continue;
2234    
2235     if (line[res - 1] == '\n')
2236        line[res - 1] = '\0';
2237     else if (len > res)
2238        line[res] = '\0';
2239     else {
2240        line = realloc(line, res + 1);
2241        line[res] = '\0';
2242     }
2243    
2244     devicePtr = line;
2245     bounds = line + res;
2246    
2247     while ((isspace(*line) && ((devicePtr + 1) <= bounds)))
2248        devicePtr++;
2249     dbgPrintf("device: %s\n", devicePtr);
2250    
2251     if (!strncmp(devicePtr, device, strlen(device))) {
2252        devicePtr += strlen(device);
2253        while (isspace(*devicePtr) && ((devicePtr + 1) <= bounds))
2254            devicePtr++;
2255    
2256        *bootPtr = strdup(devicePtr);
2257        break;
2258     }
2259        }
2260    
2261        free(line);
2262        fclose(deviceMap);
2263        return 0;
2264    }
2265    
2266    int suseGrubConfGetBoot(const char * path, char ** bootPtr) {
2267        char * grubDevice;
2268    
2269        if (suseGrubConfGetInstallDevice(path, &grubDevice))
2270     dbgPrintf("error looking for grub installation device\n");
2271        else
2272     dbgPrintf("grubby installation device: %s\n", grubDevice);
2273    
2274        if (grubGetBootFromDeviceMap(grubDevice, bootPtr))
2275     dbgPrintf("error looking for grub boot device\n");
2276        else
2277     dbgPrintf("grubby boot device: %s\n", *bootPtr);
2278    
2279        free(grubDevice);
2280        return 0;
2281    }
2282    
2283    int parseSuseGrubConf(int * lbaPtr, char ** bootPtr) {
2284        /*
2285         * This SuSE grub configuration file at this location is not your average
2286         * grub configuration file, but instead the grub commands used to setup
2287         * grub on that system.
2288         */
2289        const char * path;
2290        const static char default_path[] = "/etc/grub.conf";
2291    
2292        if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL)
2293     path = default_path;
2294    
2295        if (!isSuseGrubConf(path)) return 1;
2296    
2297        if (lbaPtr) {
2298            *lbaPtr = 0;
2299            if (suseGrubConfGetLba(path, lbaPtr))
2300                return 1;
2301        }
2302    
2303        if (bootPtr) {
2304            *bootPtr = NULL;
2305            suseGrubConfGetBoot(path, bootPtr);
2306        }
2307    
2308        return 0;
2309    }
2310    
2311  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
2312      FILE * in;      FILE * in;
2313      char buf[1024];      char buf[1024];
# Line 2092  int parseSysconfigGrub(int * lbaPtr, cha Line 2356  int parseSysconfigGrub(int * lbaPtr, cha
2356  }  }
2357    
2358  void dumpSysconfigGrub(void) {  void dumpSysconfigGrub(void) {
2359      char * boot;      char * boot = NULL;
2360      int lba;      int lba;
2361    
2362      if (!parseSysconfigGrub(&lba, &boot)) {      if (isSuseSystem()) {
2363   if (lba) printf("lba\n");          if (parseSuseGrubConf(&lba, &boot)) {
2364   if (boot) printf("boot=%s\n", boot);      free(boot);
2365        return;
2366     }
2367        } else {
2368            if (parseSysconfigGrub(&lba, &boot)) {
2369        free(boot);
2370        return;
2371     }
2372        }
2373    
2374        if (lba) printf("lba\n");
2375        if (boot) {
2376     printf("boot=%s\n", boot);
2377     free(boot);
2378      }      }
2379  }  }
2380    
# Line 2911  int checkForGrub(struct grubConfig * con Line 3188  int checkForGrub(struct grubConfig * con
3188      int fd;      int fd;
3189      unsigned char bootSect[512];      unsigned char bootSect[512];
3190      char * boot;      char * boot;
3191        int onSuse = isSuseSystem();
3192    
3193      if (parseSysconfigGrub(NULL, &boot))  
3194   return 0;      if (onSuse) {
3195     if (parseSuseGrubConf(NULL, &boot))
3196        return 0;
3197        } else {
3198     if (parseSysconfigGrub(NULL, &boot))
3199        return 0;
3200        }
3201    
3202      /* assume grub is not installed -- not an error condition */      /* assume grub is not installed -- not an error condition */
3203      if (!boot)      if (!boot)
# Line 2932  int checkForGrub(struct grubConfig * con Line 3216  int checkForGrub(struct grubConfig * con
3216      }      }
3217      close(fd);      close(fd);
3218    
3219        /* The more elaborate checks do not work on SuSE. The checks done
3220         * seem to be reasonble (at least for now), so just return success
3221         */
3222        if (onSuse)
3223     return 2;
3224    
3225      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3226  }  }
3227    
# Line 2965  int checkForExtLinux(struct grubConfig * Line 3255  int checkForExtLinux(struct grubConfig *
3255      return checkDeviceBootloader(boot, bootSect);      return checkDeviceBootloader(boot, bootSect);
3256  }  }
3257    
3258    int checkForYaboot(struct grubConfig * config) {
3259        /*
3260         * This is a simplistic check that we consider good enough for own puporses
3261         *
3262         * If we were to properly check if yaboot is *installed* we'd need to:
3263         * 1) get the system boot device (LT_BOOT)
3264         * 2) considering it's a raw filesystem, check if the yaboot binary matches
3265         *    the content on the boot device
3266         * 3) if not, copy the binary to a temporary file and run "addnote" on it
3267         * 4) check again if binary and boot device contents match
3268         */
3269        if (!access("/etc/yaboot.conf", R_OK))
3270     return 2;
3271    
3272        return 1;
3273    }
3274    
3275    int checkForElilo(struct grubConfig * config) {
3276        if (!access("/etc/elilo.conf", R_OK))
3277     return 2;
3278    
3279        return 1;
3280    }
3281    
3282  static char * getRootSpecifier(char * str) {  static char * getRootSpecifier(char * str) {
3283      char * idx, * rootspec = NULL;      char * idx, * rootspec = NULL;
3284    
# Line 3435  int main(int argc, const char ** argv) { Line 3749  int main(int argc, const char ** argv) {
3749      int displayDefault = 0;      int displayDefault = 0;
3750      int displayDefaultIndex = 0;      int displayDefaultIndex = 0;
3751      int displayDefaultTitle = 0;      int displayDefaultTitle = 0;
3752        int defaultIndex = -1;
3753      struct poptOption options[] = {      struct poptOption options[] = {
3754   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
3755      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 3452  int main(int argc, const char ** argv) { Line 3767  int main(int argc, const char ** argv) {
3767   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
3768      _("filestystem which contains /boot directory (for testing only)"),      _("filestystem which contains /boot directory (for testing only)"),
3769      _("bootfs") },      _("bootfs") },
3770  #if defined(__i386__) || defined(__x86_64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
3771   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
3772      _("check if lilo is installed on lilo.conf boot sector") },      _("check which bootloader is installed on boot sector") },
3773  #endif  #endif
3774   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,
3775      _("path to grub config file to update (\"-\" for stdin)"),      _("path to grub config file to update (\"-\" for stdin)"),
# Line 3507  int main(int argc, const char ** argv) { Line 3822  int main(int argc, const char ** argv) {
3822   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,   { "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0,
3823      _("make the first entry referencing the specified kernel "      _("make the first entry referencing the specified kernel "
3824        "the default"), _("kernel-path") },        "the default"), _("kernel-path") },
3825     { "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
3826        _("make the given entry index the default entry"),
3827        _("entry-index") },
3828   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,   { "silo", 0, POPT_ARG_NONE, &configureSilo, 0,
3829      _("configure silo bootloader") },      _("configure silo bootloader") },
3830   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   { "title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 3615  int main(int argc, const char ** argv) { Line 3933  int main(int argc, const char ** argv) {
3933      }      }
3934    
3935      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
3936    newKernelPath || removeKernelPath || makeDefault ||      newKernelPath || removeKernelPath || makeDefault ||
3937    defaultKernel || displayDefaultIndex || displayDefaultTitle)) {      defaultKernel || displayDefaultIndex || displayDefaultTitle ||
3938        (defaultIndex >= 0))) {
3939   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
3940    "specified option"));    "specified option"));
3941   return 1;   return 1;
# Line 3658  int main(int argc, const char ** argv) { Line 3977  int main(int argc, const char ** argv) {
3977   makeDefault = 1;   makeDefault = 1;
3978   defaultKernel = NULL;   defaultKernel = NULL;
3979      }      }
3980        else if (defaultKernel && (defaultIndex >= 0)) {
3981     fprintf(stderr, _("grubby: --set-default and --set-default-index "
3982      "may not be used together\n"));
3983     return 1;
3984        }
3985    
3986      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
3987   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
# Line 3666  int main(int argc, const char ** argv) { Line 3990  int main(int argc, const char ** argv) {
3990      }      }
3991    
3992      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
3993   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
3994          && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) {   && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle
3995     && (defaultIndex == -1)) {
3996   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
3997   return 1;   return 1;
3998      }      }
# Line 3694  int main(int argc, const char ** argv) { Line 4019  int main(int argc, const char ** argv) {
4019      }      }
4020    
4021      if (bootloaderProbe) {      if (bootloaderProbe) {
4022   int lrc = 0, grc = 0, gr2c = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0;
4023   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig, * yconfig, * econfig;
4024    
4025   const char *grub2config = grub2FindConfig(&grub2ConfigType);   const char *grub2config = grub2FindConfig(&grub2ConfigType);
4026   if (grub2config) {   if (grub2config) {
# Line 3723  int main(int argc, const char ** argv) { Line 4048  int main(int argc, const char ** argv) {
4048   lrc = checkForLilo(lconfig);   lrc = checkForLilo(lconfig);
4049   }   }
4050    
4051     if (!access(eliloConfigType.defaultConfig, F_OK)) {
4052        econfig = readConfig(eliloConfigType.defaultConfig,
4053     &eliloConfigType);
4054        if (!econfig)
4055     erc = 1;
4056        else
4057     erc = checkForElilo(econfig);
4058     }
4059    
4060   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {
4061      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);
4062      if (!lconfig)      if (!lconfig)
4063   erc = 1;   extrc = 1;
4064      else      else
4065   erc = checkForExtLinux(lconfig);   extrc = checkForExtLinux(lconfig);
4066   }   }
4067    
4068   if (lrc == 1 || grc == 1 || gr2c == 1) return 1;  
4069     if (!access(yabootConfigType.defaultConfig, F_OK)) {
4070        yconfig = readConfig(yabootConfigType.defaultConfig,
4071     &yabootConfigType);
4072        if (!yconfig)
4073     yrc = 1;
4074        else
4075     yrc = checkForYaboot(yconfig);
4076     }
4077    
4078     if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||
4079     erc == 1)
4080        return 1;
4081    
4082   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
4083   if (gr2c == 2) printf("grub2\n");   if (gr2c == 2) printf("grub2\n");
4084   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
4085   if (erc == 2) printf("extlinux\n");   if (extrc == 2) printf("extlinux\n");
4086     if (yrc == 2) printf("yaboot\n");
4087     if (erc == 2) printf("elilo\n");
4088    
4089   return 0;   return 0;
4090      }      }
# Line 3803  int main(int argc, const char ** argv) { Line 4151  int main(int argc, const char ** argv) {
4151      markRemovedImage(config, removeKernelPath, bootPrefix);      markRemovedImage(config, removeKernelPath, bootPrefix);
4152      markRemovedImage(config, removeMBKernel, bootPrefix);      markRemovedImage(config, removeMBKernel, bootPrefix);
4153      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,      setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault,
4154      bootPrefix, flags);      bootPrefix, flags, defaultIndex);
4155      setFallbackImage(config, newKernelPath != NULL);      setFallbackImage(config, newKernelPath != NULL);
4156      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4157                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;

Legend:
Removed from v.1844  
changed lines
  Added in v.1934