Magellan Linux

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

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

revision 1717 by niro, Sat Feb 18 00:47:17 2012 UTC revision 1840 by niro, Mon Jul 2 12:48:49 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 236  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 640  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 798  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 871  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 892  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 961  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 970  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 1035  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 1092  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 1335  static char *findDiskForRoot() Line 1506  static char *findDiskForRoot()
1506      return NULL;      return NULL;
1507  }  }
1508    
1509    void printEntry(struct singleEntry * entry) {
1510        int i;
1511        struct singleLine * line;
1512    
1513        for (line = entry->lines; line; line = line->next) {
1514     fprintf(stderr, "DBG: %s", line->indent);
1515     for (i = 0; i < line->numElements; i++) {
1516        /* Need to handle this, because we strip the quotes from
1517         * menuentry when read it. */
1518        if (line->type == LT_MENUENTRY && i == 1) {
1519     if(!isquote(*line->elements[i].item))
1520        fprintf(stderr, "\'%s\'", line->elements[i].item);
1521     else
1522        fprintf(stderr, "%s", line->elements[i].item);
1523     fprintf(stderr, "%s", line->elements[i].indent);
1524    
1525     continue;
1526        }
1527        
1528        fprintf(stderr, "%s%s",
1529        line->elements[i].item, line->elements[i].indent);
1530     }
1531     fprintf(stderr, "\n");
1532        }
1533    }
1534    
1535    void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)
1536    {
1537        va_list argp;
1538    
1539        if (!debug)
1540     return;
1541    
1542        va_start(argp, fmt);
1543        fprintf(stderr, "DBG: Image entry failed: ");
1544        vfprintf(stderr, fmt, argp);
1545        printEntry(entry);
1546        va_end(argp);
1547    }
1548    
1549    #define beginswith(s, c) ((s) && (s)[0] == (c))
1550    
1551    static int endswith(const char *s, char c)
1552    {
1553     int slen;
1554    
1555     if (!s || !s[0])
1556     return 0;
1557     slen = strlen(s) - 1;
1558    
1559     return s[slen] == c;
1560    }
1561    
1562  int suitableImage(struct singleEntry * entry, const char * bootPrefix,  int suitableImage(struct singleEntry * entry, const char * bootPrefix,
1563    int skipRemoved, int flags) {    int skipRemoved, int flags) {
1564      struct singleLine * line;      struct singleLine * line;
# Line 1344  int suitableImage(struct singleEntry * e Line 1568  int suitableImage(struct singleEntry * e
1568      char * rootspec;      char * rootspec;
1569      char * rootdev;      char * rootdev;
1570    
1571      if (skipRemoved && entry->skip) return 0;      if (skipRemoved && entry->skip) {
1572     notSuitablePrintf(entry, "marked to skip\n");
1573     return 0;
1574        }
1575    
1576      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);
1577      if (!line || line->numElements < 2) return 0;      if (!line) {
1578     notSuitablePrintf(entry, "no line found\n");
1579     return 0;
1580        }
1581        if (line->numElements < 2) {
1582     notSuitablePrintf(entry, "line has only %d elements\n",
1583        line->numElements);
1584     return 0;
1585        }
1586    
1587      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;
1588    
1589      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1590        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
1591      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1592      sprintf(fullName, "%s%s", bootPrefix,      int rootspec_offset = rootspec ? strlen(rootspec) : 0;
1593              line->elements[1].item + (rootspec ? strlen(rootspec) : 0));      int hasslash = endswith(bootPrefix, '/') ||
1594      if (access(fullName, R_OK)) return 0;       beginswith(line->elements[1].item + rootspec_offset, '/');
1595        sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1596                line->elements[1].item + rootspec_offset);
1597        if (access(fullName, R_OK)) {
1598     notSuitablePrintf(entry, "access to %s failed\n", fullName);
1599     return 0;
1600        }
1601      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
1602   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1603      if (i < line->numElements) {      if (i < line->numElements) {
# Line 1375  int suitableImage(struct singleEntry * e Line 1615  int suitableImage(struct singleEntry * e
1615      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);
1616    
1617              /* failed to find one */              /* failed to find one */
1618              if (!line) return 0;              if (!line) {
1619     notSuitablePrintf(entry, "no line found\n");
1620     return 0;
1621                }
1622    
1623      for (i = 1; i < line->numElements; i++)      for (i = 1; i < line->numElements; i++)
1624          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1625      if (i < line->numElements)      if (i < line->numElements)
1626          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1627      else {      else {
1628     notSuitablePrintf(entry, "no root= entry found\n");
1629   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1630          return 0;          return 0;
1631              }              }
# Line 1389  int suitableImage(struct singleEntry * e Line 1633  int suitableImage(struct singleEntry * e
1633      }      }
1634    
1635      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1636      if (!dev)      if (!getpathbyspec(dev)) {
1637            notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);
1638          return 0;          return 0;
1639        } else
1640     dev = getpathbyspec(dev);
1641    
1642      rootdev = findDiskForRoot();      rootdev = findDiskForRoot();
1643      if (!rootdev)      if (!rootdev) {
1644            notSuitablePrintf(entry, "can't find root device\n");
1645   return 0;   return 0;
1646        }
1647    
1648      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1649            notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",
1650     getuuidbydev(rootdev), getuuidbydev(dev));
1651          free(rootdev);          free(rootdev);
1652          return 0;          return 0;
1653      }      }
1654    
1655      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1656            notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",
1657     getuuidbydev(rootdev), getuuidbydev(dev));
1658   free(rootdev);   free(rootdev);
1659          return 0;          return 0;
1660      }      }
# Line 1488  struct singleEntry * findEntryByPath(str Line 1741  struct singleEntry * findEntryByPath(str
1741    
1742   if (!strncmp(kernel, "TITLE=", 6)) {   if (!strncmp(kernel, "TITLE=", 6)) {
1743      prefix = "";      prefix = "";
1744      checkType = LT_TITLE;      checkType = LT_TITLE|LT_MENUENTRY;
1745      kernel += 6;      kernel += 6;
1746   }   }
1747    
# Line 1504  struct singleEntry * findEntryByPath(str Line 1757  struct singleEntry * findEntryByPath(str
1757       checkType, line);       checkType, line);
1758   if (!line) break;  /* not found in this entry */   if (!line) break;  /* not found in this entry */
1759    
1760   if (line && line->numElements >= 2) {   if (line && line->type != LT_MENUENTRY &&
1761     line->numElements >= 2) {
1762      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1763      if (!strcmp(line->elements[1].item +      if (!strcmp(line->elements[1].item +
1764   ((rootspec != NULL) ? strlen(rootspec) : 0),   ((rootspec != NULL) ? strlen(rootspec) : 0),
1765   kernel + strlen(prefix)))   kernel + strlen(prefix)))
1766   break;   break;
1767   }   }
1768     if(line->type == LT_MENUENTRY &&
1769     !strcmp(line->elements[1].item, kernel))
1770        break;
1771      }      }
1772    
1773      /* make sure this entry has a kernel identifier; this skips      /* make sure this entry has a kernel identifier; this skips
# Line 1602  void markRemovedImage(struct grubConfig Line 1859  void markRemovedImage(struct grubConfig
1859        const char * prefix) {        const char * prefix) {
1860      struct singleEntry * entry;      struct singleEntry * entry;
1861    
1862      if (!image) return;      if (!image)
1863     return;
1864    
1865        /* check and see if we're removing the default image */
1866        if (isdigit(*image)) {
1867     entry = findEntryByPath(cfg, image, prefix, NULL);
1868     if(entry)
1869        entry->skip = 1;
1870     return;
1871        }
1872    
1873      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))
1874   entry->skip = 1;   entry->skip = 1;
# Line 1629  void setDefaultImage(struct grubConfig * Line 1895  void setDefaultImage(struct grubConfig *
1895    
1896      /* 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
1897         changes */         changes */
1898      if (config->defaultImage == DEFAULT_SAVED)      if ((config->defaultImage == DEFAULT_SAVED) ||
1899     (config->defaultImage == DEFAULT_SAVED_GRUB2))
1900        /* default is set to saved, we don't want to change it */        /* default is set to saved, we don't want to change it */
1901        return;        return;
1902    
# Line 1696  void displayEntry(struct singleEntry * e Line 1963  void displayEntry(struct singleEntry * e
1963          return;          return;
1964      }      }
1965    
1966      printf("kernel=%s\n", line->elements[1].item);      printf("kernel=%s%s\n", prefix, line->elements[1].item);
1967    
1968      if (line->numElements >= 3) {      if (line->numElements >= 3) {
1969   printf("args=\"");   printf("args=\"");
# Line 1760  void displayEntry(struct singleEntry * e Line 2027  void displayEntry(struct singleEntry * e
2027      printf("%s%s", line->elements[i].item, line->elements[i].indent);      printf("%s%s", line->elements[i].item, line->elements[i].indent);
2028   printf("\n");   printf("\n");
2029      }      }
2030    
2031        line = getLineByType(LT_TITLE, entry->lines);
2032        if (line) {
2033     printf("title=%s\n", line->elements[1].item);
2034        } else {
2035     char * title;
2036     line = getLineByType(LT_MENUENTRY, entry->lines);
2037     title = grub2ExtractTitle(line);
2038     if (title)
2039        printf("title=%s\n", title);
2040        }
2041  }  }
2042    
2043  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
# Line 2014  void removeLine(struct singleEntry * ent Line 2292  void removeLine(struct singleEntry * ent
2292      free(line);      free(line);
2293  }  }
2294    
 static int isquote(char q)  
 {  
     if (q == '\'' || q == '\"')  
  return 1;  
     return 0;  
 }  
   
2295  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2296  {  {
2297      struct singleLine newLine = {      struct singleLine newLine = {
# Line 2468  int checkDeviceBootloader(const char * d Line 2739  int checkDeviceBootloader(const char * d
2739      if (memcmp(boot, bootSect, 3))      if (memcmp(boot, bootSect, 3))
2740   return 0;   return 0;
2741    
2742      if (boot[1] == 0xeb) {      if (boot[1] == JMP_SHORT_OPCODE) {
2743   offset = boot[2] + 2;   offset = boot[2] + 2;
2744      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {      } else if (boot[1] == 0xe8 || boot[1] == 0xe9) {
2745   offset = (boot[3] << 8) + boot[2] + 2;   offset = (boot[3] << 8) + boot[2] + 2;
2746      } else if (boot[0] == 0xeb) {      } else if (boot[0] == JMP_SHORT_OPCODE) {
2747   offset = boot[1] + 2;        offset = boot[1] + 2;
2748            /*
2749     * it looks like grub, when copying stage1 into the mbr, patches stage1
2750     * right after the JMP location, replacing other instructions such as
2751     * JMPs for NOOPs. So, relax the check a little bit by skipping those
2752     * different bytes.
2753     */
2754          if ((bootSect[offset + 1] == NOOP_OPCODE)
2755      && (bootSect[offset + 2] == NOOP_OPCODE)) {
2756     offset = offset + 3;
2757          }
2758      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {      } else if (boot[0] == 0xe8 || boot[0] == 0xe9) {
2759   offset = (boot[2] << 8) + boot[1] + 2;   offset = (boot[2] << 8) + boot[1] + 2;
2760      } else {      } else {
# Line 2948  int addNewKernel(struct grubConfig * con Line 3229  int addNewKernel(struct grubConfig * con
3229   }   }
3230      } else if (tmplLine->type == LT_ECHO) {      } else if (tmplLine->type == LT_ECHO) {
3231      requote(tmplLine, config->cfi);      requote(tmplLine, config->cfi);
3232        static const char *prefix = "'Loading ";
3233      if (tmplLine->numElements > 1 &&      if (tmplLine->numElements > 1 &&
3234      strstr(tmplLine->elements[1].item, "'Loading Linux ")) {      strstr(tmplLine->elements[1].item, prefix) &&
3235   char *prefix = "'Loading ";      masterLine->next && masterLine->next->type == LT_KERNEL) {
3236   char *newTitle = malloc(strlen(prefix) +   char *newTitle = malloc(strlen(prefix) +
3237   strlen(newKernelTitle) + 2);   strlen(newKernelTitle) + 2);
3238    
# Line 3147  int main(int argc, const char ** argv) { Line 3429  int main(int argc, const char ** argv) {
3429      struct singleEntry * template = NULL;      struct singleEntry * template = NULL;
3430      int copyDefault = 0, makeDefault = 0;      int copyDefault = 0, makeDefault = 0;
3431      int displayDefault = 0;      int displayDefault = 0;
3432        int displayDefaultIndex = 0;
3433        int displayDefaultTitle = 0;
3434      struct poptOption options[] = {      struct poptOption options[] = {
3435   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,   { "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0,
3436      _("add an entry for the specified kernel"), _("kernel-path") },      _("add an entry for the specified kernel"), _("kernel-path") },
# Line 3177  int main(int argc, const char ** argv) { Line 3461  int main(int argc, const char ** argv) {
3461        "the kernel referenced by the default image does not exist, "        "the kernel referenced by the default image does not exist, "
3462        "the first linux entry whose kernel does exist is used as the "        "the first linux entry whose kernel does exist is used as the "
3463        "template"), NULL },        "template"), NULL },
3464     { "debug", 0, 0, &debug, 0,
3465        _("print debugging information for failures") },
3466   { "default-kernel", 0, 0, &displayDefault, 0,   { "default-kernel", 0, 0, &displayDefault, 0,
3467      _("display the path of the default kernel") },      _("display the path of the default kernel") },
3468     { "default-index", 0, 0, &displayDefaultIndex, 0,
3469        _("display the index of the default kernel") },
3470     { "default-title", 0, 0, &displayDefaultTitle, 0,
3471        _("display the title of the default kernel") },
3472   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
3473      _("configure elilo bootloader") },      _("configure elilo bootloader") },
3474   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
# Line 3295  int main(int argc, const char ** argv) { Line 3585  int main(int argc, const char ** argv) {
3585      }      }
3586    
3587      if (!cfi) {      if (!cfi) {
3588            if (grub2FindConfig(&grub2ConfigType))
3589        cfi = &grub2ConfigType;
3590     else
3591        #ifdef __ia64__        #ifdef __ia64__
3592   cfi = &eliloConfigType;      cfi = &eliloConfigType;
3593        #elif __powerpc__        #elif __powerpc__
3594   cfi = &yabootConfigType;      cfi = &yabootConfigType;
3595        #elif __sparc__        #elif __sparc__
3596          cfi = &siloConfigType;              cfi = &siloConfigType;
3597        #elif __s390__        #elif __s390__
3598          cfi = &ziplConfigType;              cfi = &ziplConfigType;
3599        #elif __s390x__        #elif __s390x__
3600          cfi = &ziplConfigtype;              cfi = &ziplConfigtype;
3601        #else        #else
         if (grub2FindConfig(&grub2ConfigType))  
     cfi = &grub2ConfigType;  
  else  
3602      cfi = &grubConfigType;      cfi = &grubConfigType;
3603        #endif        #endif
3604      }      }
# Line 3322  int main(int argc, const char ** argv) { Line 3612  int main(int argc, const char ** argv) {
3612    
3613      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
3614    newKernelPath || removeKernelPath || makeDefault ||    newKernelPath || removeKernelPath || makeDefault ||
3615    defaultKernel)) {    defaultKernel || displayDefaultIndex || displayDefaultTitle)) {
3616   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "   fprintf(stderr, _("grubby: --bootloader-probe may not be used with "
3617    "specified option"));    "specified option"));
3618   return 1;   return 1;
# Line 3373  int main(int argc, const char ** argv) { Line 3663  int main(int argc, const char ** argv) {
3663    
3664      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel      if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel
3665   && !kernelInfo && !bootloaderProbe && !updateKernelPath   && !kernelInfo && !bootloaderProbe && !updateKernelPath
3666          && !removeMBKernel) {          && !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) {
3667   fprintf(stderr, _("grubby: no action specified\n"));   fprintf(stderr, _("grubby: no action specified\n"));
3668   return 1;   return 1;
3669      }      }
# Line 3468  int main(int argc, const char ** argv) { Line 3758  int main(int argc, const char ** argv) {
3758                 ((rootspec != NULL) ? strlen(rootspec) : 0));                 ((rootspec != NULL) ? strlen(rootspec) : 0));
3759    
3760   return 0;   return 0;
3761    
3762        } else if (displayDefaultTitle) {
3763     struct singleLine * line;
3764     struct singleEntry * entry;
3765    
3766     if (config->defaultImage == -1) return 0;
3767     entry = findEntryByIndex(config, config->defaultImage);
3768     if (!entry) return 0;
3769    
3770     if (!configureGrub2) {
3771      line = getLineByType(LT_TITLE, entry->lines);
3772      if (!line) return 0;
3773      printf("%s\n", line->elements[1].item);
3774    
3775     } else {
3776      char * title;
3777    
3778      dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
3779      line = getLineByType(LT_MENUENTRY, entry->lines);
3780      if (!line) return 0;
3781      title = grub2ExtractTitle(line);
3782      if (title)
3783        printf("%s\n", title);
3784     }
3785     return 0;
3786    
3787        } else if (displayDefaultIndex) {
3788            if (config->defaultImage == -1) return 0;
3789            printf("%i\n", config->defaultImage);
3790    
3791      } else if (kernelInfo)      } else if (kernelInfo)
3792   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);
3793    

Legend:
Removed from v.1717  
changed lines
  Added in v.1840