Magellan Linux

Diff of /trunk/grubby/grubby.c

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

revision 1714 by niro, Sat Feb 18 00:32:14 2012 UTC revision 1844 by niro, Mon Jul 2 12:59:07 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 */
54  #define CODE_SEG_SIZE  128 /* code segment checked by --bootloader-probe */  #define CODE_SEG_SIZE  128 /* code segment checked by --bootloader-probe */
55    
56    #define NOOP_OPCODE 0x90
57    #define JMP_SHORT_OPCODE 0xeb
58    
59  /* comments get lumped in with indention */  /* comments get lumped in with indention */
60  struct lineElement {  struct lineElement {
61      char * item;      char * item;
# Line 109  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 156  struct keywordTypes grubKeywords[] = { Line 162  struct keywordTypes grubKeywords[] = {
162      { NULL,    0, 0 },      { NULL,    0, 0 },
163  };  };
164    
165    const char *grubFindConfig(struct configFileInfo *cfi) {
166        static const char *configFiles[] = {
167     "/etc/grub.conf",
168     "/boot/grub/grub.conf",
169     "/boot/grub/menu.lst",
170     NULL
171        };
172        static int i = -1;
173    
174        if (i == -1) {
175     for (i = 0; configFiles[i] != NULL; i++) {
176        dbgPrintf("Checking \"%s\": ", configFiles[i]);
177        if (!access(configFiles[i], R_OK)) {
178     dbgPrintf("found\n");
179     return configFiles[i];
180        }
181        dbgPrintf("not found\n");
182     }
183        }
184        return configFiles[i];
185    }
186    
187  struct configFileInfo grubConfigType = {  struct configFileInfo grubConfigType = {
188      .defaultConfig = "/boot/grub/grub.conf",      .findConfig = grubFindConfig,
189      .keywords = grubKeywords,      .keywords = grubKeywords,
190      .defaultIsIndex = 1,      .defaultIsIndex = 1,
191      .defaultSupportSaved = 1,      .defaultSupportSaved = 1,
# Line 214  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 i;
247      int count = 0;
248    
249      for (i = 0; i < line->numElements; i++) {
250        int indentSize = 0;
251    
252        count = count + strlen(line->elements[i].item);
253    
254        indentSize = strlen(line->elements[i].indent);
255        if (indentSize > 0)
256          count = count + indentSize;
257        else
258          /* be extra safe and add room for whitespaces */
259          count = count + 1;
260      }
261    
262      /* room for trailing terminator */
263      count = count + 1;
264    
265      return count;
266    }
267    
268    static int isquote(char q)
269    {
270        if (q == '\'' || q == '\"')
271     return 1;
272        return 0;
273    }
274    
275    char *grub2ExtractTitle(struct singleLine * line) {
276        char * current;
277        char * current_indent;
278        int current_len;
279        int current_indent_len;
280        int i;
281    
282        /* bail out if line does not start with menuentry */
283        if (strcmp(line->elements[0].item, "menuentry"))
284          return NULL;
285    
286        i = 1;
287        current = line->elements[i].item;
288        current_len = strlen(current);
289    
290        /* if second word is quoted, strip the quotes and return single word */
291        if (isquote(*current) && isquote(current[current_len - 1])) {
292     char *tmp;
293    
294     tmp = strdup(current);
295     *(tmp + current_len - 1) = '\0';
296     return ++tmp;
297        }
298    
299        /* if no quotes, return second word verbatim */
300        if (!isquote(*current))
301     return current;
302    
303        /* second element start with a quote, so we have to find the element
304         * whose last character is also quote (assuming it's the closing one) */
305        int resultMaxSize;
306        char * result;
307        
308        resultMaxSize = sizeOfSingleLine(line);
309        result = malloc(resultMaxSize);
310        snprintf(result, resultMaxSize, "%s", ++current);
311        
312        i++;
313        for (; i < line->numElements; ++i) {
314     current = line->elements[i].item;
315     current_len = strlen(current);
316     current_indent = line->elements[i].indent;
317     current_indent_len = strlen(current_indent);
318    
319     strncat(result, current_indent, current_indent_len);
320     if (!isquote(current[current_len-1])) {
321        strncat(result, current, current_len);
322     } else {
323        strncat(result, current, current_len - 1);
324        break;
325     }
326        }
327        return result;
328    }
329    
330  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
331      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
332      .keywords = grub2Keywords,      .keywords = grub2Keywords,
333      .defaultIsIndex = 1,      .defaultIsIndex = 1,
334      .defaultSupportSaved = 0,      .defaultSupportSaved = 1,
335      .defaultIsVariable = 1,      .defaultIsVariable = 1,
336      .entryStart = LT_MENUENTRY,      .entryStart = LT_MENUENTRY,
337      .entryEnd = LT_ENTRY_END,      .entryEnd = LT_ENTRY_END,
# Line 618  static int lineWrite(FILE * out, struct Line 731  static int lineWrite(FILE * out, struct
731      if (fprintf(out, "%s", line->indent) == -1) return -1;      if (fprintf(out, "%s", line->indent) == -1) return -1;
732    
733      for (i = 0; i < line->numElements; i++) {      for (i = 0; i < line->numElements; i++) {
734     /* Need to handle this, because we strip the quotes from
735     * menuentry when read it. */
736     if (line->type == LT_MENUENTRY && i == 1) {
737        if(!isquote(*line->elements[i].item))
738     fprintf(out, "\'%s\'", line->elements[i].item);
739        else
740     fprintf(out, "%s", line->elements[i].item);
741        fprintf(out, "%s", line->elements[i].indent);
742    
743        continue;
744     }
745    
746   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)
747      if (fputc('"', out) == EOF) return -1;      if (fputc('"', out) == EOF) return -1;
748    
# Line 776  static int getNextLine(char ** bufPtr, s Line 901  static int getNextLine(char ** bufPtr, s
901   break;   break;
902   }   }
903    
904   free(line->elements[i].indent);   line->elements[i + 1].indent = line->elements[i].indent;
905   line->elements[i].indent = strdup(indent);   line->elements[i].indent = strdup(indent);
906   *p++ = '\0';   *p++ = '\0';
907   i++;   i++;
908   line->elements[i].item = strdup(p);   line->elements[i].item = strdup(p);
  line->elements[i].indent = strdup("");  
  p = line->elements[i].item;  
909   }   }
910      }      }
911   }   }
# Line 849  static struct grubConfig * readConfig(co Line 972  static struct grubConfig * readConfig(co
972      cfg->secondaryIndent = strdup(line->indent);      cfg->secondaryIndent = strdup(line->indent);
973   }   }
974    
975   if (isEntryStart(line, cfi)) {   if (isEntryStart(line, cfi) || (cfg->entries && !sawEntry)) {
976      sawEntry = 1;      sawEntry = 1;
977      if (!entry) {      if (!entry) {
978   cfg->entries = malloc(sizeof(*entry));   cfg->entries = malloc(sizeof(*entry));
# Line 870  static struct grubConfig * readConfig(co Line 993  static struct grubConfig * readConfig(co
993      dbgPrintf("found 'set' command (%d elements): ", line->numElements);      dbgPrintf("found 'set' command (%d elements): ", line->numElements);
994      dbgPrintf("%s", line->indent);      dbgPrintf("%s", line->indent);
995      for (i = 0; i < line->numElements; i++)      for (i = 0; i < line->numElements; i++)
996   dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item);   dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent);
997      dbgPrintf("\n");      dbgPrintf("\n");
998      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
999      if (kwType && line->numElements == 3 &&      if (kwType && line->numElements == 3 &&
# Line 939  static struct grubConfig * readConfig(co Line 1062  static struct grubConfig * readConfig(co
1062      line->elements[line->numElements - 1].indent;      line->elements[line->numElements - 1].indent;
1063      line->elements[1].item = buf;      line->elements[1].item = buf;
1064      line->numElements = 2;      line->numElements = 2;
1065     } else if (line->type == LT_MENUENTRY && line->numElements > 3) {
1066        /* let --remove-kernel="TITLE=what" work */
1067        len = 0;
1068        char *extras;
1069        char *title;
1070    
1071        for (i = 1; i < line->numElements; i++) {
1072     len += strlen(line->elements[i].item);
1073     len += strlen(line->elements[i].indent);
1074        }
1075        buf = malloc(len + 1);
1076        *buf = '\0';
1077    
1078        /* allocate mem for extra flags. */
1079        extras = malloc(len + 1);
1080        *extras = '\0';
1081    
1082        /* get title. */
1083        for (i = 0; i < line->numElements; i++) {
1084     if (!strcmp(line->elements[i].item, "menuentry"))
1085        continue;
1086     if (isquote(*line->elements[i].item))
1087        title = line->elements[i].item + 1;
1088     else
1089        title = line->elements[i].item;
1090    
1091     len = strlen(title);
1092            if (isquote(title[len-1])) {
1093        strncat(buf, title,len-1);
1094        break;
1095     } else {
1096        strcat(buf, title);
1097        strcat(buf, line->elements[i].indent);
1098     }
1099        }
1100    
1101        /* get extras */
1102        int count = 0;
1103        for (i = 0; i < line->numElements; i++) {
1104     if (count >= 2) {
1105        strcat(extras, line->elements[i].item);
1106        strcat(extras, line->elements[i].indent);
1107     }
1108    
1109     if (!strcmp(line->elements[i].item, "menuentry"))
1110        continue;
1111    
1112     /* count ' or ", there should be two in menuentry line. */
1113     if (isquote(*line->elements[i].item))
1114        count++;
1115    
1116     len = strlen(line->elements[i].item);
1117    
1118     if (isquote(line->elements[i].item[len -1]))
1119        count++;
1120    
1121     /* ok, we get the final ' or ", others are extras. */
1122                }
1123        line->elements[1].indent =
1124     line->elements[line->numElements - 2].indent;
1125        line->elements[1].item = buf;
1126        line->elements[2].indent =
1127     line->elements[line->numElements - 2].indent;
1128        line->elements[2].item = extras;
1129        line->numElements = 3;
1130   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {
1131      /* Strip off any " which may be present; they'll be put back      /* Strip off any " which may be present; they'll be put back
1132         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 948  static struct grubConfig * readConfig(co Line 1135  static struct grubConfig * readConfig(co
1135      if (line->numElements >= 2) {      if (line->numElements >= 2) {
1136   int last, len;   int last, len;
1137    
1138   if (*line->elements[1].item == '"')   if (isquote(*line->elements[1].item))
1139      memmove(line->elements[1].item, line->elements[1].item + 1,      memmove(line->elements[1].item, line->elements[1].item + 1,
1140      strlen(line->elements[1].item + 1) + 1);      strlen(line->elements[1].item + 1) + 1);
1141    
1142   last = line->numElements - 1;   last = line->numElements - 1;
1143   len = strlen(line->elements[last].item) - 1;   len = strlen(line->elements[last].item) - 1;
1144   if (line->elements[last].item[len] == '"')   if (isquote(line->elements[last].item[len]))
1145      line->elements[last].item[len] = '\0';      line->elements[last].item[len] = '\0';
1146      }      }
1147   }   }
# Line 1013  static struct grubConfig * readConfig(co Line 1200  static struct grubConfig * readConfig(co
1200    
1201      dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");      dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1202      if (defaultLine) {      if (defaultLine) {
1203   if (cfi->defaultIsVariable) {          if (defaultLine->numElements > 2 &&
1204        cfi->defaultSupportSaved &&
1205        !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1206        cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1207     } else if (cfi->defaultIsVariable) {
1208      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1209      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
1210      *value == ' ' || *value == '\t'))      *value == ' ' || *value == '\t'))
# Line 1070  static void writeDefault(FILE * out, cha Line 1261  static void writeDefault(FILE * out, cha
1261    
1262      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1263   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1264        else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)
1265     fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1266      else if (cfg->defaultImage > -1) {      else if (cfg->defaultImage > -1) {
1267   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1268      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
# Line 1268  static char *findDiskForRoot() Line 1461  static char *findDiskForRoot()
1461      buf[rc] = '\0';      buf[rc] = '\0';
1462      chptr = buf;      chptr = buf;
1463    
1464        char *foundanswer = NULL;
1465    
1466      while (chptr && chptr != buf+rc) {      while (chptr && chptr != buf+rc) {
1467          devname = chptr;          devname = chptr;
1468    
# Line 1295  static char *findDiskForRoot() Line 1490  static char *findDiskForRoot()
1490           * for '/' obviously.           * for '/' obviously.
1491           */           */
1492          if (*(++chptr) == '/' && *(++chptr) == ' ') {          if (*(++chptr) == '/' && *(++chptr) == ' ') {
1493              /*              /* remember the last / entry in mtab */
1494               * 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);  
1495          }          }
1496    
1497          /* Next line */          /* Next line */
# Line 1310  static char *findDiskForRoot() Line 1500  static char *findDiskForRoot()
1500              chptr++;              chptr++;
1501      }      }
1502    
1503        /* Return the last / entry found */
1504        if (foundanswer) {
1505            chptr = strchr(foundanswer, ' ');
1506            *chptr = '\0';
1507            return strdup(foundanswer);
1508        }
1509    
1510      return NULL;      return NULL;
1511  }  }
1512    
1513    void printEntry(struct singleEntry * entry) {
1514        int i;
1515        struct singleLine * line;
1516    
1517        for (line = entry->lines; line; line = line->next) {
1518     fprintf(stderr, "DBG: %s", line->indent);
1519     for (i = 0; i < line->numElements; i++) {
1520        /* Need to handle this, because we strip the quotes from
1521         * menuentry when read it. */
1522        if (line->type == LT_MENUENTRY && i == 1) {
1523     if(!isquote(*line->elements[i].item))
1524        fprintf(stderr, "\'%s\'", line->elements[i].item);
1525     else
1526        fprintf(stderr, "%s", line->elements[i].item);
1527     fprintf(stderr, "%s", line->elements[i].indent);
1528    
1529     continue;
1530        }
1531        
1532        fprintf(stderr, "%s%s",
1533        line->elements[i].item, line->elements[i].indent);
1534     }
1535     fprintf(stderr, "\n");
1536        }
1537    }
1538    
1539    void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)
1540    {
1541        va_list argp;
1542    
1543        if (!debug)
1544     return;
1545    
1546        va_start(argp, fmt);
1547        fprintf(stderr, "DBG: Image entry failed: ");
1548        vfprintf(stderr, fmt, argp);
1549        printEntry(entry);
1550        va_end(argp);
1551    }
1552    
1553    #define beginswith(s, c) ((s) && (s)[0] == (c))
1554    
1555    static int endswith(const char *s, char c)
1556    {
1557     int slen;
1558    
1559     if (!s || !s[0])
1560     return 0;
1561     slen = strlen(s) - 1;
1562    
1563     return s[slen] == c;
1564    }
1565    
1566  int suitableImage(struct singleEntry * entry, const char * bootPrefix,  int suitableImage(struct singleEntry * entry, const char * bootPrefix,
1567    int skipRemoved, int flags) {    int skipRemoved, int flags) {
1568      struct singleLine * line;      struct singleLine * line;
# Line 1322  int suitableImage(struct singleEntry * e Line 1572  int suitableImage(struct singleEntry * e
1572      char * rootspec;      char * rootspec;
1573      char * rootdev;      char * rootdev;
1574    
1575      if (skipRemoved && entry->skip) return 0;      if (skipRemoved && entry->skip) {
1576     notSuitablePrintf(entry, "marked to skip\n");
1577     return 0;
1578        }
1579    
1580      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);
1581      if (!line || line->numElements < 2) return 0;      if (!line) {
1582     notSuitablePrintf(entry, "no line found\n");
1583     return 0;
1584        }
1585        if (line->numElements < 2) {
1586     notSuitablePrintf(entry, "line has only %d elements\n",
1587        line->numElements);
1588     return 0;
1589        }
1590    
1591      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;
1592    
1593      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1594        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
1595      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1596      sprintf(fullName, "%s%s", bootPrefix,      int rootspec_offset = rootspec ? strlen(rootspec) : 0;
1597              line->elements[1].item + (rootspec ? strlen(rootspec) : 0));      int hasslash = endswith(bootPrefix, '/') ||
1598      if (access(fullName, R_OK)) return 0;       beginswith(line->elements[1].item + rootspec_offset, '/');
1599        sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1600                line->elements[1].item + rootspec_offset);
1601        if (access(fullName, R_OK)) {
1602     notSuitablePrintf(entry, "access to %s failed\n", fullName);
1603     return 0;
1604        }
1605      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
1606   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1607      if (i < line->numElements) {      if (i < line->numElements) {
# Line 1353  int suitableImage(struct singleEntry * e Line 1619  int suitableImage(struct singleEntry * e
1619      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);
1620    
1621              /* failed to find one */              /* failed to find one */
1622              if (!line) return 0;              if (!line) {
1623     notSuitablePrintf(entry, "no line found\n");
1624     return 0;
1625                }
1626    
1627      for (i = 1; i < line->numElements; i++)      for (i = 1; i < line->numElements; i++)
1628          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1629      if (i < line->numElements)      if (i < line->numElements)
1630          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1631      else {      else {
1632     notSuitablePrintf(entry, "no root= entry found\n");
1633   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1634          return 0;          return 0;
1635              }              }
# Line 1367  int suitableImage(struct singleEntry * e Line 1637  int suitableImage(struct singleEntry * e
1637      }      }
1638    
1639      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1640      if (!dev)      if (!getpathbyspec(dev)) {
1641            notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);
1642          return 0;          return 0;
1643        } else
1644     dev = getpathbyspec(dev);
1645    
1646      rootdev = findDiskForRoot();      rootdev = findDiskForRoot();
1647      if (!rootdev)      if (!rootdev) {
1648            notSuitablePrintf(entry, "can't find root device\n");
1649   return 0;   return 0;
1650        }
1651    
1652      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1653            notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",
1654     getuuidbydev(rootdev), getuuidbydev(dev));
1655          free(rootdev);          free(rootdev);
1656          return 0;          return 0;
1657      }      }
1658    
1659      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1660            notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",
1661     getuuidbydev(rootdev), getuuidbydev(dev));
1662   free(rootdev);   free(rootdev);
1663          return 0;          return 0;
1664      }      }
# Line 1466  struct singleEntry * findEntryByPath(str Line 1745  struct singleEntry * findEntryByPath(str
1745    
1746   if (!strncmp(kernel, "TITLE=", 6)) {   if (!strncmp(kernel, "TITLE=", 6)) {
1747      prefix = "";      prefix = "";
1748      checkType = LT_TITLE;      checkType = LT_TITLE|LT_MENUENTRY;
1749      kernel += 6;      kernel += 6;
1750   }   }
1751    
# Line 1482  struct singleEntry * findEntryByPath(str Line 1761  struct singleEntry * findEntryByPath(str
1761       checkType, line);       checkType, line);
1762   if (!line) break;  /* not found in this entry */   if (!line) break;  /* not found in this entry */
1763    
1764   if (line && line->numElements >= 2) {   if (line && line->type != LT_MENUENTRY &&
1765     line->numElements >= 2) {
1766      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1767      if (!strcmp(line->elements[1].item +      if (!strcmp(line->elements[1].item +
1768   ((rootspec != NULL) ? strlen(rootspec) : 0),   ((rootspec != NULL) ? strlen(rootspec) : 0),
1769   kernel + strlen(prefix)))   kernel + strlen(prefix)))
1770   break;   break;
1771   }   }
1772     if(line->type == LT_MENUENTRY &&
1773     !strcmp(line->elements[1].item, kernel))
1774        break;
1775      }      }
1776    
1777      /* make sure this entry has a kernel identifier; this skips      /* make sure this entry has a kernel identifier; this skips
# Line 1580  void markRemovedImage(struct grubConfig Line 1863  void markRemovedImage(struct grubConfig
1863        const char * prefix) {        const char * prefix) {
1864      struct singleEntry * entry;      struct singleEntry * entry;
1865    
1866      if (!image) return;      if (!image)
1867     return;
1868    
1869        /* check and see if we're removing the default image */
1870        if (isdigit(*image)) {
1871     entry = findEntryByPath(cfg, image, prefix, NULL);
1872     if(entry)
1873        entry->skip = 1;
1874     return;
1875        }
1876    
1877      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))
1878   entry->skip = 1;   entry->skip = 1;
# Line 1607  void setDefaultImage(struct grubConfig * Line 1899  void setDefaultImage(struct grubConfig *
1899    
1900      /* 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
1901         changes */         changes */
1902      if (config->defaultImage == DEFAULT_SAVED)      if ((config->defaultImage == DEFAULT_SAVED) ||
1903     (config->defaultImage == DEFAULT_SAVED_GRUB2))
1904        /* default is set to saved, we don't want to change it */        /* default is set to saved, we don't want to change it */
1905        return;        return;
1906    
# Line 1674  void displayEntry(struct singleEntry * e Line 1967  void displayEntry(struct singleEntry * e
1967          return;          return;
1968      }      }
1969    
1970      printf("kernel=%s\n", line->elements[1].item);      printf("kernel=%s%s\n", prefix, line->elements[1].item);
1971    
1972      if (line->numElements >= 3) {      if (line->numElements >= 3) {
1973   printf("args=\"");   printf("args=\"");
# Line 1738  void displayEntry(struct singleEntry * e Line 2031  void displayEntry(struct singleEntry * e
2031      printf("%s%s", line->elements[i].item, line->elements[i].indent);      printf("%s%s", line->elements[i].item, line->elements[i].indent);
2032   printf("\n");   printf("\n");
2033      }      }
2034    
2035        line = getLineByType(LT_TITLE, entry->lines);
2036        if (line) {
2037     printf("title=%s\n", line->elements[1].item);
2038        } else {
2039     char * title;
2040     line = getLineByType(LT_MENUENTRY, entry->lines);
2041     title = grub2ExtractTitle(line);
2042     if (title)
2043        printf("title=%s\n", title);
2044        }
2045  }  }
2046    
2047  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
# Line 1992  void removeLine(struct singleEntry * ent Line 2296  void removeLine(struct singleEntry * ent
2296      free(line);      free(line);
2297  }  }
2298    
 static int isquote(char q)  
 {  
     if (q == '\'' || q == '\"')  
  return 1;  
     return 0;  
 }  
   
2299  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2300  {  {
2301      struct singleLine newLine = {      struct singleLine newLine = {
# Line 2446  int checkDeviceBootloader(const char * d Line 2743  int checkDeviceBootloader(const char * d
2743      if (memcmp(boot, bootSect, 3))      if (memcmp(boot, bootSect, 3))
2744   return 0;   return 0;
2745    
2746      if (boot[1] == 0xeb) {      if (boot[1] == JMP_SHORT_OPCODE) {
2747   offset = boot[2] + 2;   offset = boot[2] + 2;
2748      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {
2749   offset = (boot[3] << 8) + boot[2] + 2;   offset = (boot[3] << 8) + boot[2] + 2;
2750      } else if (boot[0] == 0xeb) {      } else if (boot[0] == JMP_SHORT_OPCODE) {
2751   offset = boot[1] + 2;        offset = boot[1] + 2;
2752            /*
2753     * it looks like grub, when copying stage1 into the mbr, patches stage1
2754     * right after the JMP location, replacing other instructions such as
2755     * JMPs for NOOPs. So, relax the check a little bit by skipping those
2756     * different bytes.
2757     */
2758          if ((bootSect[offset + 1] == NOOP_OPCODE)
2759      && (bootSect[offset + 2] == NOOP_OPCODE)) {
2760     offset = offset + 3;
2761          }
2762      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {
2763   offset = (boot[2] << 8) + boot[1] + 2;   offset = (boot[2] << 8) + boot[1] + 2;
2764      } else {      } else {
# Line 2672  static char * getRootSpecifier(char * st Line 2979  static char * getRootSpecifier(char * st
2979  static char * getInitrdVal(struct grubConfig * config,  static char * getInitrdVal(struct grubConfig * config,
2980     const char * prefix, struct singleLine *tmplLine,     const char * prefix, struct singleLine *tmplLine,
2981     const char * newKernelInitrd,     const char * newKernelInitrd,
2982     char ** extraInitrds, int extraInitrdCount)     const char ** extraInitrds, int extraInitrdCount)
2983  {  {
2984      char *initrdVal, *end;      char *initrdVal, *end;
2985      int i;      int i;
# Line 2717  static char * getInitrdVal(struct grubCo Line 3024  static char * getInitrdVal(struct grubCo
3024    
3025  int addNewKernel(struct grubConfig * config, struct singleEntry * template,  int addNewKernel(struct grubConfig * config, struct singleEntry * template,
3026           const char * prefix,           const char * prefix,
3027   char * newKernelPath, char * newKernelTitle,   const char * newKernelPath, const char * newKernelTitle,
3028   char * newKernelArgs, char * newKernelInitrd,   const char * newKernelArgs, const char * newKernelInitrd,
3029   char ** extraInitrds, int extraInitrdCount,   const char ** extraInitrds, int extraInitrdCount,
3030                   char * newMBKernel, char * newMBKernelArgs) {                   const char * newMBKernel, const char * newMBKernelArgs) {
3031      struct singleEntry * new;      struct singleEntry * new;
3032      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;
3033      int needs;      int needs;
# Line 2926  int addNewKernel(struct grubConfig * con Line 3233  int addNewKernel(struct grubConfig * con
3233   }   }
3234      } else if (tmplLine->type == LT_ECHO) {      } else if (tmplLine->type == LT_ECHO) {
3235      requote(tmplLine, config->cfi);      requote(tmplLine, config->cfi);
3236        static const char *prefix = "'Loading ";
3237      if (tmplLine->numElements > 1 &&      if (tmplLine->numElements > 1 &&
3238      strstr(tmplLine->elements[1].item, "'Loading Linux ")) {      strstr(tmplLine->elements[1].item, prefix) &&
3239   char *prefix = "'Loading ";      masterLine->next && masterLine->next->type == LT_KERNEL) {
3240   char *newTitle = malloc(strlen(prefix) +   char *newTitle = malloc(strlen(prefix) +
3241   strlen(newKernelTitle) + 2);   strlen(newKernelTitle) + 2);
3242    
# Line 3125  int main(int argc, const char ** argv) { Line 3433  int main(int argc, const char ** argv) {
3433      struct singleEntry * template = NULL;      struct singleEntry * template = NULL;
3434      int copyDefault = 0, makeDefault = 0;      int copyDefault = 0, makeDefault = 0;
3435      int displayDefault = 0;      int displayDefault = 0;
3436        int displayDefaultIndex = 0;
3437        int displayDefaultTitle = 0;
3438      struct poptOption options[] = {      struct poptOption options[] = {
3439   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
3440      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 3155  int main(int argc, const char ** argv) { Line 3465  int main(int argc, const char ** argv) {
3465        "the kernel referenced by the default image does not exist, "        "the kernel referenced by the default image does not exist, "
3466        "the first linux entry whose kernel does exist is used as the "        "the first linux entry whose kernel does exist is used as the "
3467        "template"), NULL },        "template"), NULL },
3468     { "debug", 0, 0, &debug, 0,
3469        _("print debugging information for failures") },
3470   { "default-kernel", 0, 0, &displayDefault, 0,   { "default-kernel", 0, 0, &displayDefault, 0,
3471      _("display the path of the default kernel") },      _("display the path of the default kernel") },
3472     { "default-index", 0, 0, &displayDefaultIndex, 0,
3473        _("display the index of the default kernel") },
3474     { "default-title", 0, 0, &displayDefaultTitle, 0,
3475        _("display the title of the default kernel") },
3476   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
3477      _("configure elilo bootloader") },      _("configure elilo bootloader") },
3478   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
# Line 3273  int main(int argc, const char ** argv) { Line 3589  int main(int argc, const char ** argv) {
3589      }      }
3590    
3591      if (!cfi) {      if (!cfi) {
3592            if (grub2FindConfig(&grub2ConfigType))
3593        cfi = &grub2ConfigType;
3594     else
3595        #ifdef __ia64__        #ifdef __ia64__
3596   cfi = &eliloConfigType;      cfi = &eliloConfigType;
3597        #elif __powerpc__        #elif __powerpc__
3598   cfi = &yabootConfigType;      cfi = &yabootConfigType;
3599        #elif __sparc__        #elif __sparc__
3600          cfi = &siloConfigType;              cfi = &siloConfigType;
3601        #elif __s390__        #elif __s390__
3602          cfi = &ziplConfigType;              cfi = &ziplConfigType;
3603        #elif __s390x__        #elif __s390x__
3604          cfi = &ziplConfigtype;              cfi = &ziplConfigtype;
3605        #else        #else
         if (grub2FindConfig(&grub2ConfigType))  
     cfi = &grub2ConfigType;  
  else  
3606      cfi = &grubConfigType;      cfi = &grubConfigType;
3607        #endif        #endif
3608      }      }
# Line 3300  int main(int argc, const char ** argv) { Line 3616  int main(int argc, const char ** argv) {
3616    
3617      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
3618    newKernelPath || removeKernelPath || makeDefault ||    newKernelPath || removeKernelPath || makeDefault ||
3619    defaultKernel)) {    defaultKernel || displayDefaultIndex || displayDefaultTitle)) {
3620   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
3621    "specified option"));    "specified option"));
3622   return 1;   return 1;
# Line 3343  int main(int argc, const char ** argv) { Line 3659  int main(int argc, const char ** argv) {
3659   defaultKernel = NULL;   defaultKernel = NULL;
3660      }      }
3661    
3662      if (!strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
3663   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
3664   "is used\n"));   "is used\n"));
3665   return 1;   return 1;
# Line 3351  int main(int argc, const char ** argv) { Line 3667  int main(int argc, const char ** argv) {
3667    
3668      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
3669   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
3670          && !removeMBKernel) {          && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) {
3671   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
3672   return 1;   return 1;
3673      }      }
# Line 3390  int main(int argc, const char ** argv) { Line 3706  int main(int argc, const char ** argv) {
3706   gr2c = checkForGrub2(gconfig);   gr2c = checkForGrub2(gconfig);
3707   }   }
3708    
3709   if (!access(grubConfigType.defaultConfig, F_OK)) {   const char *grubconfig = grubFindConfig(&grubConfigType);
3710      gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType);   if (!access(grubconfig, F_OK)) {
3711        gconfig = readConfig(grubconfig, &grubConfigType);
3712      if (!gconfig)      if (!gconfig)
3713   grc = 1;   grc = 1;
3714      else      else
# Line 3445  int main(int argc, const char ** argv) { Line 3762  int main(int argc, const char ** argv) {
3762                 ((rootspec != NULL) ? strlen(rootspec) : 0));                 ((rootspec != NULL) ? strlen(rootspec) : 0));
3763    
3764   return 0;   return 0;
3765    
3766        } else if (displayDefaultTitle) {
3767     struct singleLine * line;
3768     struct singleEntry * entry;
3769    
3770     if (config->defaultImage == -1) return 0;
3771     entry = findEntryByIndex(config, config->defaultImage);
3772     if (!entry) return 0;
3773    
3774     if (!configureGrub2) {
3775      line = getLineByType(LT_TITLE, entry->lines);
3776      if (!line) return 0;
3777      printf("%s\n", line->elements[1].item);
3778    
3779     } else {
3780      char * title;
3781    
3782      dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
3783      line = getLineByType(LT_MENUENTRY, entry->lines);
3784      if (!line) return 0;
3785      title = grub2ExtractTitle(line);
3786      if (title)
3787        printf("%s\n", title);
3788     }
3789     return 0;
3790    
3791        } else if (displayDefaultIndex) {
3792            if (config->defaultImage == -1) return 0;
3793            printf("%i\n", config->defaultImage);
3794    
3795      } else if (kernelInfo)      } else if (kernelInfo)
3796   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
3797    
# Line 3466  int main(int argc, const char ** argv) { Line 3813  int main(int argc, const char ** argv) {
3813      }      }
3814      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
3815                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,
3816                       extraInitrds, extraInitrdCount,                       (const char **)extraInitrds, extraInitrdCount,
3817                       newMBKernel, newMBKernelArgs)) return 1;                       newMBKernel, newMBKernelArgs)) return 1;
3818            
3819    

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