Magellan Linux

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

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

revision 1727 by niro, Sat Feb 18 00:57:49 2012 UTC revision 1854 by niro, Mon Jul 2 13:12:32 2012 UTC
# Line 46  Line 46 
46  #define dbgPrintf(format, args...)  #define dbgPrintf(format, args...)
47  #endif  #endif
48    
49    int debug = 0; /* Currently just for template debugging */
50    
51  #define _(A) (A)  #define _(A) (A)
52    
53  #define MAX_EXTRA_INITRDS  16 /* code segment checked by --bootloader-probe */  #define MAX_EXTRA_INITRDS  16 /* code segment checked by --bootloader-probe */
# Line 112  struct singleEntry { Line 114  struct singleEntry {
114    
115  #define MAIN_DEFAULT    (1 << 0)  #define MAIN_DEFAULT    (1 << 0)
116  #define DEFAULT_SAVED       -2  #define DEFAULT_SAVED       -2
117    #define DEFAULT_SAVED_GRUB2 -3
118    
119  struct keywordTypes {  struct keywordTypes {
120      char * key;      char * key;
# Line 161  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 239  const char *grub2FindConfig(struct confi Line 242  const char *grub2FindConfig(struct confi
242      return configFiles[i];      return configFiles[i];
243  }  }
244    
245    int sizeOfSingleLine(struct singleLine * line) {
246      int count = 0;
247    
248      for (int i = 0; i < line->numElements; i++) {
249        int indentSize = 0;
250    
251        count = count + strlen(line->elements[i].item);
252    
253        indentSize = strlen(line->elements[i].indent);
254        if (indentSize > 0)
255          count = count + indentSize;
256        else
257          /* be extra safe and add room for whitespaces */
258          count = count + 1;
259      }
260    
261      /* room for trailing terminator */
262      count = count + 1;
263    
264      return count;
265    }
266    
267    static int isquote(char q)
268    {
269        if (q == '\'' || q == '\"')
270     return 1;
271        return 0;
272    }
273    
274    char *grub2ExtractTitle(struct singleLine * line) {
275        char * current;
276        char * current_indent;
277        int current_len;
278        int current_indent_len;
279        int i;
280    
281        /* bail out if line does not start with menuentry */
282        if (strcmp(line->elements[0].item, "menuentry"))
283          return NULL;
284    
285        i = 1;
286        current = line->elements[i].item;
287        current_len = strlen(current);
288    
289        /* if second word is quoted, strip the quotes and return single word */
290        if (isquote(*current) && isquote(current[current_len - 1])) {
291     char *tmp;
292    
293     tmp = strdup(current);
294     *(tmp + current_len - 1) = '\0';
295     return ++tmp;
296        }
297    
298        /* if no quotes, return second word verbatim */
299        if (!isquote(*current))
300     return current;
301    
302        /* second element start with a quote, so we have to find the element
303         * whose last character is also quote (assuming it's the closing one) */
304        int resultMaxSize;
305        char * result;
306        
307        resultMaxSize = sizeOfSingleLine(line);
308        result = malloc(resultMaxSize);
309        snprintf(result, resultMaxSize, "%s", ++current);
310        
311        i++;
312        for (; i < line->numElements; ++i) {
313     current = line->elements[i].item;
314     current_len = strlen(current);
315     current_indent = line->elements[i].indent;
316     current_indent_len = strlen(current_indent);
317    
318     strncat(result, current_indent, current_indent_len);
319     if (!isquote(current[current_len-1])) {
320        strncat(result, current, current_len);
321     } else {
322        strncat(result, current, current_len - 1);
323        break;
324     }
325        }
326        return result;
327    }
328    
329  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
330      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
331      .keywords = grub2Keywords,      .keywords = grub2Keywords,
332      .defaultIsIndex = 1,      .defaultIsIndex = 1,
333      .defaultSupportSaved = 0,      .defaultSupportSaved = 1,
334      .defaultIsVariable = 1,      .defaultIsVariable = 1,
335      .entryStart = LT_MENUENTRY,      .entryStart = LT_MENUENTRY,
336      .entryEnd = LT_ENTRY_END,      .entryEnd = LT_ENTRY_END,
# Line 485  static char * sdupprintf(const char *for Line 572  static char * sdupprintf(const char *for
572    
573  static struct keywordTypes * getKeywordByType(enum lineType_e type,  static struct keywordTypes * getKeywordByType(enum lineType_e type,
574        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
575      struct keywordTypes * kw;      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
     for (kw = cfi->keywords; kw->key; kw++) {  
576   if (kw->type == type)   if (kw->type == type)
577      return kw;      return kw;
578      }      }
# Line 516  static char * getuuidbydev(char *device) Line 602  static char * getuuidbydev(char *device)
602    
603  static enum lineType_e getTypeByKeyword(char * keyword,  static enum lineType_e getTypeByKeyword(char * keyword,
604   struct configFileInfo * cfi) {   struct configFileInfo * cfi) {
605      struct keywordTypes * kw;      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
     for (kw = cfi->keywords; kw->key; kw++) {  
606   if (!strcmp(keyword, kw->key))   if (!strcmp(keyword, kw->key))
607      return kw->type;      return kw->type;
608      }      }
# Line 604  static void lineInit(struct singleLine * Line 689  static void lineInit(struct singleLine *
689  }  }
690    
691  struct singleLine * lineDup(struct singleLine * line) {  struct singleLine * lineDup(struct singleLine * line) {
     int i;  
692      struct singleLine * newLine = malloc(sizeof(*newLine));      struct singleLine * newLine = malloc(sizeof(*newLine));
693    
694      newLine->indent = strdup(line->indent);      newLine->indent = strdup(line->indent);
# Line 614  struct singleLine * lineDup(struct singl Line 698  struct singleLine * lineDup(struct singl
698      newLine->elements = malloc(sizeof(*newLine->elements) *      newLine->elements = malloc(sizeof(*newLine->elements) *
699         newLine->numElements);         newLine->numElements);
700    
701      for (i = 0; i < newLine->numElements; i++) {      for (int i = 0; i < newLine->numElements; i++) {
702   newLine->elements[i].indent = strdup(line->elements[i].indent);   newLine->elements[i].indent = strdup(line->elements[i].indent);
703   newLine->elements[i].item = strdup(line->elements[i].item);   newLine->elements[i].item = strdup(line->elements[i].item);
704      }      }
# Line 623  struct singleLine * lineDup(struct singl Line 707  struct singleLine * lineDup(struct singl
707  }  }
708    
709  static void lineFree(struct singleLine * line) {  static void lineFree(struct singleLine * line) {
     int i;  
   
710      if (line->indent) free(line->indent);      if (line->indent) free(line->indent);
711    
712      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
713   free(line->elements[i].item);   free(line->elements[i].item);
714   free(line->elements[i].indent);   free(line->elements[i].indent);
715      }      }
# Line 638  static void lineFree(struct singleLine * Line 720  static void lineFree(struct singleLine *
720    
721  static int lineWrite(FILE * out, struct singleLine * line,  static int lineWrite(FILE * out, struct singleLine * line,
722       struct configFileInfo * cfi) {       struct configFileInfo * cfi) {
     int i;  
   
723      if (fprintf(out, "%s", line->indent) == -1) return -1;      if (fprintf(out, "%s", line->indent) == -1) return -1;
724    
725      for (i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
726     /* Need to handle this, because we strip the quotes from
727     * menuentry when read it. */
728     if (line->type == LT_MENUENTRY && i == 1) {
729        if(!isquote(*line->elements[i].item))
730     fprintf(out, "\'%s\'", line->elements[i].item);
731        else
732     fprintf(out, "%s", line->elements[i].item);
733        fprintf(out, "%s", line->elements[i].indent);
734    
735        continue;
736     }
737    
738   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)
739      if (fputc('"', out) == EOF) return -1;      if (fputc('"', out) == EOF) return -1;
740    
# Line 736  static int getNextLine(char ** bufPtr, s Line 828  static int getNextLine(char ** bufPtr, s
828      if (*line->elements[0].item == '#') {      if (*line->elements[0].item == '#') {
829   char * fullLine;   char * fullLine;
830   int len;   int len;
  int i;  
831    
832   len = strlen(line->indent);   len = strlen(line->indent);
833   for (i = 0; i < line->numElements; i++)   for (int i = 0; i < line->numElements; i++)
834      len += strlen(line->elements[i].item) +      len += strlen(line->elements[i].item) +
835     strlen(line->elements[i].indent);     strlen(line->elements[i].indent);
836    
# Line 748  static int getNextLine(char ** bufPtr, s Line 839  static int getNextLine(char ** bufPtr, s
839   free(line->indent);   free(line->indent);
840   line->indent = fullLine;   line->indent = fullLine;
841    
842   for (i = 0; i < line->numElements; i++) {   for (int i = 0; i < line->numElements; i++) {
843      strcat(fullLine, line->elements[i].item);      strcat(fullLine, line->elements[i].item);
844      strcat(fullLine, line->elements[i].indent);      strcat(fullLine, line->elements[i].indent);
845      free(line->elements[i].item);      free(line->elements[i].item);
# Line 767  static int getNextLine(char ** bufPtr, s Line 858  static int getNextLine(char ** bufPtr, s
858   * elements up more   * elements up more
859   */   */
860   if (!isspace(kw->separatorChar)) {   if (!isspace(kw->separatorChar)) {
     int i;  
861      char indent[2] = "";      char indent[2] = "";
862      indent[0] = kw->separatorChar;      indent[0] = kw->separatorChar;
863      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
864   char *p;   char *p;
  int j;  
865   int numNewElements;   int numNewElements;
866    
867   numNewElements = 0;   numNewElements = 0;
# Line 788  static int getNextLine(char ** bufPtr, s Line 877  static int getNextLine(char ** bufPtr, s
877      sizeof(*line->elements) * elementsAlloced);      sizeof(*line->elements) * elementsAlloced);
878   }   }
879    
880   for (j = line->numElements; j > i; j--) {   for (int j = line->numElements; j > i; j--) {
881   line->elements[j + numNewElements] = line->elements[j];   line->elements[j + numNewElements] = line->elements[j];
882   }   }
883   line->numElements += numNewElements;   line->numElements += numNewElements;
# Line 801  static int getNextLine(char ** bufPtr, s Line 890  static int getNextLine(char ** bufPtr, s
890   break;   break;
891   }   }
892    
893   free(line->elements[i].indent);   line->elements[i + 1].indent = line->elements[i].indent;
894   line->elements[i].indent = strdup(indent);   line->elements[i].indent = strdup(indent);
895   *p++ = '\0';   *p++ = '\0';
896   i++;   i++;
897   line->elements[i].item = strdup(p);   line->elements[i].item = strdup(p);
  line->elements[i].indent = strdup("");  
  p = line->elements[i].item;  
898   }   }
899      }      }
900   }   }
# Line 828  static struct grubConfig * readConfig(co Line 915  static struct grubConfig * readConfig(co
915      struct singleLine * last = NULL, * line, * defaultLine = NULL;      struct singleLine * last = NULL, * line, * defaultLine = NULL;
916      char * end;      char * end;
917      struct singleEntry * entry = NULL;      struct singleEntry * entry = NULL;
918      int i, len;      int len;
919      char * buf;      char * buf;
920    
921      if (!strcmp(inName, "-")) {      if (!strcmp(inName, "-")) {
# Line 891  static struct grubConfig * readConfig(co Line 978  static struct grubConfig * readConfig(co
978   }   }
979    
980   if (line->type == LT_SET_VARIABLE) {   if (line->type == LT_SET_VARIABLE) {
     int i;  
981      dbgPrintf("found 'set' command (%d elements): ", line->numElements);      dbgPrintf("found 'set' command (%d elements): ", line->numElements);
982      dbgPrintf("%s", line->indent);      dbgPrintf("%s", line->indent);
983      for (i = 0; i < line->numElements; i++)      for (int i = 0; i < line->numElements; i++)
984   dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item);   dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent);
985      dbgPrintf("\n");      dbgPrintf("\n");
986      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
987      if (kwType && line->numElements == 3 &&      if (kwType && line->numElements == 3 &&
# Line 922  static struct grubConfig * readConfig(co Line 1008  static struct grubConfig * readConfig(co
1008       * This only applies to grub, but that's the only place we       * This only applies to grub, but that's the only place we
1009       * should find LT_MBMODULE lines anyway.       * should find LT_MBMODULE lines anyway.
1010       */       */
1011      struct singleLine * l;      for (struct singleLine *l = entry->lines; l; l = l->next) {
     for (l = entry->lines; l; l = l->next) {  
1012   if (l->type == LT_HYPER)   if (l->type == LT_HYPER)
1013      break;      break;
1014   else if (l->type == LT_KERNEL) {   else if (l->type == LT_KERNEL) {
# Line 943  static struct grubConfig * readConfig(co Line 1028  static struct grubConfig * readConfig(co
1028   } else if (line->type == LT_TITLE && line->numElements > 1) {   } else if (line->type == LT_TITLE && line->numElements > 1) {
1029      /* make the title a single argument (undoing our parsing) */      /* make the title a single argument (undoing our parsing) */
1030      len = 0;      len = 0;
1031      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1032   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
1033   len += strlen(line->elements[i].indent);   len += strlen(line->elements[i].indent);
1034      }      }
1035      buf = malloc(len + 1);      buf = malloc(len + 1);
1036      *buf = '\0';      *buf = '\0';
1037    
1038      for (i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1039   strcat(buf, line->elements[i].item);   strcat(buf, line->elements[i].item);
1040   free(line->elements[i].item);   free(line->elements[i].item);
1041    
# Line 964  static struct grubConfig * readConfig(co Line 1049  static struct grubConfig * readConfig(co
1049      line->elements[line->numElements - 1].indent;      line->elements[line->numElements - 1].indent;
1050      line->elements[1].item = buf;      line->elements[1].item = buf;
1051      line->numElements = 2;      line->numElements = 2;
1052     } else if (line->type == LT_MENUENTRY && line->numElements > 3) {
1053        /* let --remove-kernel="TITLE=what" work */
1054        len = 0;
1055        char *extras;
1056        char *title;
1057    
1058        for (int i = 1; i < line->numElements; i++) {
1059     len += strlen(line->elements[i].item);
1060     len += strlen(line->elements[i].indent);
1061        }
1062        buf = malloc(len + 1);
1063        *buf = '\0';
1064    
1065        /* allocate mem for extra flags. */
1066        extras = malloc(len + 1);
1067        *extras = '\0';
1068    
1069        /* get title. */
1070        for (int i = 0; i < line->numElements; i++) {
1071     if (!strcmp(line->elements[i].item, "menuentry"))
1072        continue;
1073     if (isquote(*line->elements[i].item))
1074        title = line->elements[i].item + 1;
1075     else
1076        title = line->elements[i].item;
1077    
1078     len = strlen(title);
1079            if (isquote(title[len-1])) {
1080        strncat(buf, title,len-1);
1081        break;
1082     } else {
1083        strcat(buf, title);
1084        strcat(buf, line->elements[i].indent);
1085     }
1086        }
1087    
1088        /* get extras */
1089        int count = 0;
1090        for (int i = 0; i < line->numElements; i++) {
1091     if (count >= 2) {
1092        strcat(extras, line->elements[i].item);
1093        strcat(extras, line->elements[i].indent);
1094     }
1095    
1096     if (!strcmp(line->elements[i].item, "menuentry"))
1097        continue;
1098    
1099     /* count ' or ", there should be two in menuentry line. */
1100     if (isquote(*line->elements[i].item))
1101        count++;
1102    
1103     len = strlen(line->elements[i].item);
1104    
1105     if (isquote(line->elements[i].item[len -1]))
1106        count++;
1107    
1108     /* ok, we get the final ' or ", others are extras. */
1109                }
1110        line->elements[1].indent =
1111     line->elements[line->numElements - 2].indent;
1112        line->elements[1].item = buf;
1113        line->elements[2].indent =
1114     line->elements[line->numElements - 2].indent;
1115        line->elements[2].item = extras;
1116        line->numElements = 3;
1117   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {
1118      /* Strip off any " which may be present; they'll be put back      /* Strip off any " which may be present; they'll be put back
1119         on write. This is one of the few (the only?) places that grubby         on write. This is one of the few (the only?) places that grubby
# Line 973  static struct grubConfig * readConfig(co Line 1122  static struct grubConfig * readConfig(co
1122      if (line->numElements >= 2) {      if (line->numElements >= 2) {
1123   int last, len;   int last, len;
1124    
1125   if (*line->elements[1].item == '"')   if (isquote(*line->elements[1].item))
1126      memmove(line->elements[1].item, line->elements[1].item + 1,      memmove(line->elements[1].item, line->elements[1].item + 1,
1127      strlen(line->elements[1].item + 1) + 1);      strlen(line->elements[1].item + 1) + 1);
1128    
1129   last = line->numElements - 1;   last = line->numElements - 1;
1130   len = strlen(line->elements[last].item) - 1;   len = strlen(line->elements[last].item) - 1;
1131   if (line->elements[last].item[len] == '"')   if (isquote(line->elements[last].item[len]))
1132      line->elements[last].item[len] = '\0';      line->elements[last].item[len] = '\0';
1133      }      }
1134   }   }
# Line 1038  static struct grubConfig * readConfig(co Line 1187  static struct grubConfig * readConfig(co
1187    
1188      dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");      dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1189      if (defaultLine) {      if (defaultLine) {
1190   if (cfi->defaultIsVariable) {          if (defaultLine->numElements > 2 &&
1191        cfi->defaultSupportSaved &&
1192        !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1193        cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1194     } else if (cfi->defaultIsVariable) {
1195      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1196      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
1197      *value == ' ' || *value == '\t'))      *value == ' ' || *value == '\t'))
# Line 1055  static struct grubConfig * readConfig(co Line 1208  static struct grubConfig * readConfig(co
1208      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);      cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10);
1209      if (*end) cfg->defaultImage = -1;      if (*end) cfg->defaultImage = -1;
1210   } else if (defaultLine->numElements >= 2) {   } else if (defaultLine->numElements >= 2) {
1211      i = 0;      int i = 0;
1212      while ((entry = findEntryByIndex(cfg, i))) {      while ((entry = findEntryByIndex(cfg, i))) {
1213   for (line = entry->lines; line; line = line->next)   for (line = entry->lines; line; line = line->next)
1214      if (line->type == LT_TITLE) break;      if (line->type == LT_TITLE) break;
# Line 1095  static void writeDefault(FILE * out, cha Line 1248  static void writeDefault(FILE * out, cha
1248    
1249      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1250   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1251        else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)
1252     fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1253      else if (cfg->defaultImage > -1) {      else if (cfg->defaultImage > -1) {
1254   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1255      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
# Line 1155  static int writeConfig(struct grubConfig Line 1310  static int writeConfig(struct grubConfig
1310    
1311      /* most likely the symlink is relative, so change our      /* most likely the symlink is relative, so change our
1312         directory to the dir of the symlink */         directory to the dir of the symlink */
1313              rc = chdir(dirname(strdupa(outName)));              rc = chdir(dirname(outName));
1314      do {      do {
1315   buf = alloca(len + 1);   buf = alloca(len + 1);
1316   rc = readlink(basename(outName), buf, len);   rc = readlink(basename(outName), buf, len);
# Line 1293  static char *findDiskForRoot() Line 1448  static char *findDiskForRoot()
1448      buf[rc] = '\0';      buf[rc] = '\0';
1449      chptr = buf;      chptr = buf;
1450    
1451        char *foundanswer = NULL;
1452    
1453      while (chptr && chptr != buf+rc) {      while (chptr && chptr != buf+rc) {
1454          devname = chptr;          devname = chptr;
1455    
# Line 1320  static char *findDiskForRoot() Line 1477  static char *findDiskForRoot()
1477           * for '/' obviously.           * for '/' obviously.
1478           */           */
1479          if (*(++chptr) == '/' && *(++chptr) == ' ') {          if (*(++chptr) == '/' && *(++chptr) == ' ') {
1480              /*              /* remember the last / entry in mtab */
1481               * Move back 2, which is the first space after the device name, set             foundanswer = devname;
              * it to \0 so strdup will just get the devicename.  
              */  
             chptr -= 2;  
             *chptr = '\0';  
             return strdup(devname);  
1482          }          }
1483    
1484          /* Next line */          /* Next line */
# Line 1335  static char *findDiskForRoot() Line 1487  static char *findDiskForRoot()
1487              chptr++;              chptr++;
1488      }      }
1489    
1490        /* Return the last / entry found */
1491        if (foundanswer) {
1492            chptr = strchr(foundanswer, ' ');
1493            *chptr = '\0';
1494            return strdup(foundanswer);
1495        }
1496    
1497      return NULL;      return NULL;
1498  }  }
1499    
1500    void printEntry(struct singleEntry * entry) {
1501        int i;
1502        struct singleLine * line;
1503    
1504        for (line = entry->lines; line; line = line->next) {
1505     fprintf(stderr, "DBG: %s", line->indent);
1506     for (i = 0; i < line->numElements; i++) {
1507        /* Need to handle this, because we strip the quotes from
1508         * menuentry when read it. */
1509        if (line->type == LT_MENUENTRY && i == 1) {
1510     if(!isquote(*line->elements[i].item))
1511        fprintf(stderr, "\'%s\'", line->elements[i].item);
1512     else
1513        fprintf(stderr, "%s", line->elements[i].item);
1514     fprintf(stderr, "%s", line->elements[i].indent);
1515    
1516     continue;
1517        }
1518        
1519        fprintf(stderr, "%s%s",
1520        line->elements[i].item, line->elements[i].indent);
1521     }
1522     fprintf(stderr, "\n");
1523        }
1524    }
1525    
1526    void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)
1527    {
1528        va_list argp;
1529    
1530        if (!debug)
1531     return;
1532    
1533        va_start(argp, fmt);
1534        fprintf(stderr, "DBG: Image entry failed: ");
1535        vfprintf(stderr, fmt, argp);
1536        printEntry(entry);
1537        va_end(argp);
1538    }
1539    
1540    #define beginswith(s, c) ((s) && (s)[0] == (c))
1541    
1542    static int endswith(const char *s, char c)
1543    {
1544     int slen;
1545    
1546     if (!s || !s[0])
1547     return 0;
1548     slen = strlen(s) - 1;
1549    
1550     return s[slen] == c;
1551    }
1552    
1553  int suitableImage(struct singleEntry * entry, const char * bootPrefix,  int suitableImage(struct singleEntry * entry, const char * bootPrefix,
1554    int skipRemoved, int flags) {    int skipRemoved, int flags) {
1555      struct singleLine * line;      struct singleLine * line;
# Line 1347  int suitableImage(struct singleEntry * e Line 1559  int suitableImage(struct singleEntry * e
1559      char * rootspec;      char * rootspec;
1560      char * rootdev;      char * rootdev;
1561    
1562      if (skipRemoved && entry->skip) return 0;      if (skipRemoved && entry->skip) {
1563     notSuitablePrintf(entry, "marked to skip\n");
1564     return 0;
1565        }
1566    
1567      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);
1568      if (!line || line->numElements < 2) return 0;      if (!line) {
1569     notSuitablePrintf(entry, "no line found\n");
1570     return 0;
1571        }
1572        if (line->numElements < 2) {
1573     notSuitablePrintf(entry, "line has only %d elements\n",
1574        line->numElements);
1575     return 0;
1576        }
1577    
1578      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;
1579    
1580      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1581        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
1582      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1583      sprintf(fullName, "%s%s", bootPrefix,      int rootspec_offset = rootspec ? strlen(rootspec) : 0;
1584              line->elements[1].item + (rootspec ? strlen(rootspec) : 0));      int hasslash = endswith(bootPrefix, '/') ||
1585      if (access(fullName, R_OK)) return 0;       beginswith(line->elements[1].item + rootspec_offset, '/');
1586        sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1587                line->elements[1].item + rootspec_offset);
1588        if (access(fullName, R_OK)) {
1589     notSuitablePrintf(entry, "access to %s failed\n", fullName);
1590     return 0;
1591        }
1592      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
1593   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1594      if (i < line->numElements) {      if (i < line->numElements) {
# Line 1378  int suitableImage(struct singleEntry * e Line 1606  int suitableImage(struct singleEntry * e
1606      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);
1607    
1608              /* failed to find one */              /* failed to find one */
1609              if (!line) return 0;              if (!line) {
1610     notSuitablePrintf(entry, "no line found\n");
1611     return 0;
1612                }
1613    
1614      for (i = 1; i < line->numElements; i++)      for (i = 1; i < line->numElements; i++)
1615          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1616      if (i < line->numElements)      if (i < line->numElements)
1617          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1618      else {      else {
1619     notSuitablePrintf(entry, "no root= entry found\n");
1620   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1621          return 0;          return 0;
1622              }              }
# Line 1392  int suitableImage(struct singleEntry * e Line 1624  int suitableImage(struct singleEntry * e
1624      }      }
1625    
1626      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1627      if (!dev)      if (!getpathbyspec(dev)) {
1628            notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);
1629          return 0;          return 0;
1630        } else
1631     dev = getpathbyspec(dev);
1632    
1633      rootdev = findDiskForRoot();      rootdev = findDiskForRoot();
1634      if (!rootdev)      if (!rootdev) {
1635            notSuitablePrintf(entry, "can't find root device\n");
1636   return 0;   return 0;
1637        }
1638    
1639      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1640            notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",
1641     getuuidbydev(rootdev), getuuidbydev(dev));
1642          free(rootdev);          free(rootdev);
1643          return 0;          return 0;
1644      }      }
1645    
1646      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1647            notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",
1648     getuuidbydev(rootdev), getuuidbydev(dev));
1649   free(rootdev);   free(rootdev);
1650          return 0;          return 0;
1651      }      }
# Line 1491  struct singleEntry * findEntryByPath(str Line 1732  struct singleEntry * findEntryByPath(str
1732    
1733   if (!strncmp(kernel, "TITLE=", 6)) {   if (!strncmp(kernel, "TITLE=", 6)) {
1734      prefix = "";      prefix = "";
1735      checkType = LT_TITLE;      checkType = LT_TITLE|LT_MENUENTRY;
1736      kernel += 6;      kernel += 6;
1737   }   }
1738    
# Line 1507  struct singleEntry * findEntryByPath(str Line 1748  struct singleEntry * findEntryByPath(str
1748       checkType, line);       checkType, line);
1749   if (!line) break;  /* not found in this entry */   if (!line) break;  /* not found in this entry */
1750    
1751   if (line && line->numElements >= 2) {   if (line && line->type != LT_MENUENTRY &&
1752     line->numElements >= 2) {
1753      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1754      if (!strcmp(line->elements[1].item +      if (!strcmp(line->elements[1].item +
1755   ((rootspec != NULL) ? strlen(rootspec) : 0),   ((rootspec != NULL) ? strlen(rootspec) : 0),
1756   kernel + strlen(prefix)))   kernel + strlen(prefix)))
1757   break;   break;
1758   }   }
1759     if(line->type == LT_MENUENTRY &&
1760     !strcmp(line->elements[1].item, kernel))
1761        break;
1762      }      }
1763    
1764      /* make sure this entry has a kernel identifier; this skips      /* make sure this entry has a kernel identifier; this skips
# Line 1605  void markRemovedImage(struct grubConfig Line 1850  void markRemovedImage(struct grubConfig
1850        const char * prefix) {        const char * prefix) {
1851      struct singleEntry * entry;      struct singleEntry * entry;
1852    
1853      if (!image) return;      if (!image)
1854     return;
1855    
1856        /* check and see if we're removing the default image */
1857        if (isdigit(*image)) {
1858     entry = findEntryByPath(cfg, image, prefix, NULL);
1859     if(entry)
1860        entry->skip = 1;
1861     return;
1862        }
1863    
1864      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))
1865   entry->skip = 1;   entry->skip = 1;
# Line 1632  void setDefaultImage(struct grubConfig * Line 1886  void setDefaultImage(struct grubConfig *
1886    
1887      /* defaultImage now points to what we'd like to use, but before any order      /* defaultImage now points to what we'd like to use, but before any order
1888         changes */         changes */
1889      if (config->defaultImage == DEFAULT_SAVED)      if ((config->defaultImage == DEFAULT_SAVED) ||
1890     (config->defaultImage == DEFAULT_SAVED_GRUB2))
1891        /* default is set to saved, we don't want to change it */        /* default is set to saved, we don't want to change it */
1892        return;        return;
1893    
# Line 1699  void displayEntry(struct singleEntry * e Line 1954  void displayEntry(struct singleEntry * e
1954          return;          return;
1955      }      }
1956    
1957      printf("kernel=%s\n", line->elements[1].item);      if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
1958     printf("kernel=%s\n", line->elements[1].item);
1959        else
1960     printf("kernel=%s%s\n", prefix, line->elements[1].item);
1961    
1962      if (line->numElements >= 3) {      if (line->numElements >= 3) {
1963   printf("args=\"");   printf("args=\"");
# Line 1758  void displayEntry(struct singleEntry * e Line 2016  void displayEntry(struct singleEntry * e
2016      line = getLineByType(LT_INITRD, entry->lines);      line = getLineByType(LT_INITRD, entry->lines);
2017    
2018      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2019   printf("initrd=%s", prefix);   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
2020        printf("initrd=");
2021     else
2022        printf("initrd=%s", prefix);
2023    
2024   for (i = 1; i < line->numElements; i++)   for (i = 1; i < line->numElements; i++)
2025      printf("%s%s", line->elements[i].item, line->elements[i].indent);      printf("%s%s", line->elements[i].item, line->elements[i].indent);
2026   printf("\n");   printf("\n");
2027      }      }
2028    
2029        line = getLineByType(LT_TITLE, entry->lines);
2030        if (line) {
2031     printf("title=%s\n", line->elements[1].item);
2032        } else {
2033     char * title;
2034     line = getLineByType(LT_MENUENTRY, entry->lines);
2035     title = grub2ExtractTitle(line);
2036     if (title)
2037        printf("title=%s\n", title);
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) {
# Line 1813  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 2017  void removeLine(struct singleEntry * ent Line 2564  void removeLine(struct singleEntry * ent
2564      free(line);      free(line);
2565  }  }
2566    
 static int isquote(char q)  
 {  
     if (q == '\'' || q == '\"')  
  return 1;  
     return 0;  
 }  
   
2567  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2568  {  {
2569      struct singleLine newLine = {      struct singleLine newLine = {
# Line 2639  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 2660  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 2693  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    int checkForElilo(struct grubConfig * config) {
3267        if (!access("/etc/elilo.conf", R_OK))
3268     return 2;
3269    
3270        return 1;
3271    }
3272    
3273  static char * getRootSpecifier(char * str) {  static char * getRootSpecifier(char * str) {
3274      char * idx, * rootspec = NULL;      char * idx, * rootspec = NULL;
3275    
# Line 2707  static char * getRootSpecifier(char * st Line 3284  static char * getRootSpecifier(char * st
3284  static char * getInitrdVal(struct grubConfig * config,  static char * getInitrdVal(struct grubConfig * config,
3285     const char * prefix, struct singleLine *tmplLine,     const char * prefix, struct singleLine *tmplLine,
3286     const char * newKernelInitrd,     const char * newKernelInitrd,
3287     char ** extraInitrds, int extraInitrdCount)     const char ** extraInitrds, int extraInitrdCount)
3288  {  {
3289      char *initrdVal, *end;      char *initrdVal, *end;
3290      int i;      int i;
# Line 2752  static char * getInitrdVal(struct grubCo Line 3329  static char * getInitrdVal(struct grubCo
3329    
3330  int addNewKernel(struct grubConfig * config, struct singleEntry * template,  int addNewKernel(struct grubConfig * config, struct singleEntry * template,
3331           const char * prefix,           const char * prefix,
3332   char * newKernelPath, char * newKernelTitle,   const char * newKernelPath, const char * newKernelTitle,
3333   char * newKernelArgs, char * newKernelInitrd,   const char * newKernelArgs, const char * newKernelInitrd,
3334   char ** extraInitrds, int extraInitrdCount,   const char ** extraInitrds, int extraInitrdCount,
3335                   char * newMBKernel, char * newMBKernelArgs) {                   const char * newMBKernel, const char * newMBKernelArgs) {
3336      struct singleEntry * new;      struct singleEntry * new;
3337      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;
3338      int needs;      int needs;
# Line 3180  int main(int argc, const char ** argv) { Line 3757  int main(int argc, const char ** argv) {
3757   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   { "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
3758      _("filestystem which contains /boot directory (for testing only)"),      _("filestystem which contains /boot directory (for testing only)"),
3759      _("bootfs") },      _("bootfs") },
3760  #if defined(__i386__) || defined(__x86_64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
3761   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   { "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
3762      _("check if lilo is installed on lilo.conf boot sector") },      _("check which bootloader is installed on boot sector") },
3763  #endif  #endif
3764   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,   { "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0,
3765      _("path to grub config file to update (\"-\" for stdin)"),      _("path to grub config file to update (\"-\" for stdin)"),
# Line 3193  int main(int argc, const char ** argv) { Line 3770  int main(int argc, const char ** argv) {
3770        "the kernel referenced by the default image does not exist, "        "the kernel referenced by the default image does not exist, "
3771        "the first linux entry whose kernel does exist is used as the "        "the first linux entry whose kernel does exist is used as the "
3772        "template"), NULL },        "template"), NULL },
3773     { "debug", 0, 0, &debug, 0,
3774        _("print debugging information for failures") },
3775   { "default-kernel", 0, 0, &displayDefault, 0,   { "default-kernel", 0, 0, &displayDefault, 0,
3776      _("display the path of the default kernel") },      _("display the path of the default kernel") },
3777   { "default-index", 0, 0, &displayDefaultIndex, 0,   { "default-index", 0, 0, &displayDefaultIndex, 0,
# Line 3315  int main(int argc, const char ** argv) { Line 3894  int main(int argc, const char ** argv) {
3894      }      }
3895    
3896      if (!cfi) {      if (!cfi) {
3897            if (grub2FindConfig(&grub2ConfigType))
3898        cfi = &grub2ConfigType;
3899     else
3900        #ifdef __ia64__        #ifdef __ia64__
3901   cfi = &eliloConfigType;      cfi = &eliloConfigType;
3902        #elif __powerpc__        #elif __powerpc__
3903   cfi = &yabootConfigType;      cfi = &yabootConfigType;
3904        #elif __sparc__        #elif __sparc__
3905          cfi = &siloConfigType;              cfi = &siloConfigType;
3906        #elif __s390__        #elif __s390__
3907          cfi = &ziplConfigType;              cfi = &ziplConfigType;
3908        #elif __s390x__        #elif __s390x__
3909          cfi = &ziplConfigtype;              cfi = &ziplConfigtype;
3910        #else        #else
         if (grub2FindConfig(&grub2ConfigType))  
     cfi = &grub2ConfigType;  
  else  
3911      cfi = &grubConfigType;      cfi = &grubConfigType;
3912        #endif        #endif
3913      }      }
# Line 3420  int main(int argc, const char ** argv) { Line 3999  int main(int argc, const char ** argv) {
3999      }      }
4000    
4001      if (bootloaderProbe) {      if (bootloaderProbe) {
4002   int lrc = 0, grc = 0, gr2c = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0;
4003   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig, * yconfig, * econfig;
4004    
4005   const char *grub2config = grub2FindConfig(&grub2ConfigType);   const char *grub2config = grub2FindConfig(&grub2ConfigType);
4006   if (grub2config) {   if (grub2config) {
# Line 3449  int main(int argc, const char ** argv) { Line 4028  int main(int argc, const char ** argv) {
4028   lrc = checkForLilo(lconfig);   lrc = checkForLilo(lconfig);
4029   }   }
4030    
4031     if (!access(eliloConfigType.defaultConfig, F_OK)) {
4032        econfig = readConfig(eliloConfigType.defaultConfig,
4033     &eliloConfigType);
4034        if (!econfig)
4035     erc = 1;
4036        else
4037     erc = checkForElilo(econfig);
4038     }
4039    
4040   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {   if (!access(extlinuxConfigType.defaultConfig, F_OK)) {
4041      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);      lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType);
4042      if (!lconfig)      if (!lconfig)
4043   erc = 1;   extrc = 1;
4044      else      else
4045   erc = checkForExtLinux(lconfig);   extrc = checkForExtLinux(lconfig);
4046   }   }
4047    
4048   if (lrc == 1 || grc == 1 || gr2c == 1) return 1;  
4049     if (!access(yabootConfigType.defaultConfig, F_OK)) {
4050        yconfig = readConfig(yabootConfigType.defaultConfig,
4051     &yabootConfigType);
4052        if (!yconfig)
4053     yrc = 1;
4054        else
4055          yrc = checkForYaboot(lconfig);
4056     }
4057    
4058     if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 ||
4059        erc == 1)
4060        return 1;
4061    
4062   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
4063   if (gr2c == 2) printf("grub2\n");   if (gr2c == 2) printf("grub2\n");
4064   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
4065   if (erc == 2) printf("extlinux\n");   if (extrc == 2) printf("extlinux\n");
4066     if (yrc == 2) printf("yaboot\n");
4067     if (erc == 2) printf("elilo\n");
4068    
4069   return 0;   return 0;
4070      }      }
# Line 3503  int main(int argc, const char ** argv) { Line 4105  int main(int argc, const char ** argv) {
4105    printf("%s\n", line->elements[1].item);    printf("%s\n", line->elements[1].item);
4106    
4107   } else {   } else {
4108    int i;    char * title;
   size_t len;  
   char * start;  
   char * tmp;  
4109    
4110    dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");    dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
4111    line = getLineByType(LT_MENUENTRY, entry->lines);    line = getLineByType(LT_MENUENTRY, entry->lines);
4112    if (!line) return 0;    if (!line) return 0;
4113      title = grub2ExtractTitle(line);
4114    for (i = 0; i < line->numElements; i++) {    if (title)
4115        printf("%s\n", title);
     if (!strcmp(line->elements[i].item, "menuentry"))  
       continue;  
   
     if (*line->elements[i].item == '\'')  
       start = line->elements[i].item + 1;  
     else  
       start = line->elements[i].item;  
   
     len = strlen(start);  
     if (*(start + len - 1) == '\'') {  
       tmp = strdup(start);  
       *(tmp + len - 1) = '\0';  
       printf("%s", tmp);  
       free(tmp);  
       break;  
     } else {  
       printf("%s ", start);  
     }  
   }  
   printf("\n");  
4116   }   }
4117   return 0;   return 0;
4118    
# Line 3562  int main(int argc, const char ** argv) { Line 4141  int main(int argc, const char ** argv) {
4141      }      }
4142      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
4143                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,
4144                       extraInitrds, extraInitrdCount,                       (const char **)extraInitrds, extraInitrdCount,
4145                       newMBKernel, newMBKernelArgs)) return 1;                       newMBKernel, newMBKernelArgs)) return 1;
4146            
4147    

Legend:
Removed from v.1727  
changed lines
  Added in v.1854