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 1748 by niro, Sat Feb 18 01:07:15 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 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 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    char *grub2ExtractTitle(struct singleLine * line) {
269        char * current;
270        char * current_indent;
271        int current_len;
272        int current_indent_len;
273        int i;
274    
275        /* bail out if line does not start with menuentry */
276        if (strcmp(line->elements[0].item, "menuentry"))
277          return NULL;
278    
279        i = 1;
280        current = line->elements[i].item;
281        current_len = strlen(current);
282    
283        /* if second word is quoted, strip the quotes and return single word */
284        if ((*current == '\'') && (*(current + current_len - 1) == '\'')) {
285          char *tmp;
286    
287          tmp = strdup(current);
288          *(tmp + current_len - 1) = '\0';
289          return ++tmp;
290        }
291    
292        /* if no quotes, return second word verbatim */
293        if (*current != '\'') {
294          return current;
295        }
296    
297        /* second element start with a quote, so we have to find the element
298         * whose last character is also quote (assuming it's the closing one) */
299        if (*current == '\'') {
300          int resultMaxSize;
301          char * result;
302    
303          resultMaxSize = sizeOfSingleLine(line);
304          result = malloc(resultMaxSize);
305          snprintf(result, resultMaxSize, "%s", ++current);
306    
307          i++;
308          for (; i < line->numElements; ++i) {
309     current = line->elements[i].item;
310     current_len = strlen(current);
311     current_indent = line->elements[i].indent;
312     current_indent_len = strlen(current_indent);
313    
314     strncat(result, current_indent, current_indent_len);
315     if (*(current + current_len - 1) != '\'') {
316      strncat(result, current, current_len);
317     } else {
318      strncat(result, current, current_len - 1);
319      break;
320     }
321          }
322          return result;
323        }
324    
325        return NULL;
326    }
327    
328  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
329      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
330      .keywords = grub2Keywords,      .keywords = grub2Keywords,
331      .defaultIsIndex = 1,      .defaultIsIndex = 1,
332      .defaultSupportSaved = 0,      .defaultSupportSaved = 1,
333      .defaultIsVariable = 1,      .defaultIsVariable = 1,
334      .entryStart = LT_MENUENTRY,      .entryStart = LT_MENUENTRY,
335      .entryEnd = LT_ENTRY_END,      .entryEnd = LT_ENTRY_END,
# Line 1038  static struct grubConfig * readConfig(co Line 1124  static struct grubConfig * readConfig(co
1124    
1125      dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");      dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1126      if (defaultLine) {      if (defaultLine) {
1127   if (cfi->defaultIsVariable) {          if (defaultLine->numElements > 2 &&
1128        cfi->defaultSupportSaved &&
1129        !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1130        cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1131     } else if (cfi->defaultIsVariable) {
1132      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1133      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
1134      *value == ' ' || *value == '\t'))      *value == ' ' || *value == '\t'))
# Line 1095  static void writeDefault(FILE * out, cha Line 1185  static void writeDefault(FILE * out, cha
1185    
1186      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1187   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1188        else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)
1189     fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1190      else if (cfg->defaultImage > -1) {      else if (cfg->defaultImage > -1) {
1191   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1192      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
# Line 1338  static char *findDiskForRoot() Line 1430  static char *findDiskForRoot()
1430      return NULL;      return NULL;
1431  }  }
1432    
1433    void printEntry(struct singleEntry * entry) {
1434        int i;
1435        struct singleLine * line;
1436    
1437        for (line = entry->lines; line; line = line->next) {
1438     fprintf(stderr, "DBG: %s", line->indent);
1439     for (i = 0; i < line->numElements; i++) {
1440     fprintf(stderr, "%s%s",
1441        line->elements[i].item, line->elements[i].indent);
1442     }
1443     fprintf(stderr, "\n");
1444        }
1445    }
1446    
1447    void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...)
1448    {
1449        va_list argp;
1450    
1451        if (!debug)
1452     return;
1453    
1454        va_start(argp, fmt);
1455        fprintf(stderr, "DBG: Image entry failed: ");
1456        vfprintf(stderr, fmt, argp);
1457        printEntry(entry);
1458        va_end(argp);
1459    }
1460    
1461    #define beginswith(s, c) ((s) && (s)[0] == (c))
1462    
1463    static int endswith(const char *s, char c)
1464    {
1465     int slen;
1466    
1467     if (!s && !s[0])
1468     return 0;
1469     slen = strlen(s) - 1;
1470    
1471     return s[slen] == c;
1472    }
1473    
1474  int suitableImage(struct singleEntry * entry, const char * bootPrefix,  int suitableImage(struct singleEntry * entry, const char * bootPrefix,
1475    int skipRemoved, int flags) {    int skipRemoved, int flags) {
1476      struct singleLine * line;      struct singleLine * line;
# Line 1347  int suitableImage(struct singleEntry * e Line 1480  int suitableImage(struct singleEntry * e
1480      char * rootspec;      char * rootspec;
1481      char * rootdev;      char * rootdev;
1482    
1483      if (skipRemoved && entry->skip) return 0;      if (skipRemoved && entry->skip) {
1484     notSuitablePrintf(entry, "marked to skip\n");
1485     return 0;
1486        }
1487    
1488      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);
1489      if (!line || line->numElements < 2) return 0;      if (!line) {
1490     notSuitablePrintf(entry, "no line found\n");
1491     return 0;
1492        }
1493        if (line->numElements < 2) {
1494     notSuitablePrintf(entry, "line has only %d elements\n",
1495        line->numElements);
1496     return 0;
1497        }
1498    
1499      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;      if (flags & GRUBBY_BADIMAGE_OKAY) return 1;
1500    
1501      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1502        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
1503      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1504      sprintf(fullName, "%s%s", bootPrefix,      int rootspec_offset = rootspec ? strlen(rootspec) : 0;
1505              line->elements[1].item + (rootspec ? strlen(rootspec) : 0));      int hasslash = endswith(bootPrefix, '/') ||
1506      if (access(fullName, R_OK)) return 0;       beginswith(line->elements[1].item + rootspec_offset, '/');
1507        sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1508                line->elements[1].item + rootspec_offset);
1509        if (access(fullName, R_OK)) {
1510     notSuitablePrintf(entry, "access to %s failed\n", fullName);
1511     return 0;
1512        }
1513      for (i = 2; i < line->numElements; i++)      for (i = 2; i < line->numElements; i++)
1514   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;   if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1515      if (i < line->numElements) {      if (i < line->numElements) {
# Line 1378  int suitableImage(struct singleEntry * e Line 1527  int suitableImage(struct singleEntry * e
1527      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines);
1528    
1529              /* failed to find one */              /* failed to find one */
1530              if (!line) return 0;              if (!line) {
1531     notSuitablePrintf(entry, "no line found\n");
1532     return 0;
1533                }
1534    
1535      for (i = 1; i < line->numElements; i++)      for (i = 1; i < line->numElements; i++)
1536          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;          if (!strncasecmp(line->elements[i].item, "root=", 5)) break;
1537      if (i < line->numElements)      if (i < line->numElements)
1538          dev = line->elements[i].item + 5;          dev = line->elements[i].item + 5;
1539      else {      else {
1540     notSuitablePrintf(entry, "no root= entry found\n");
1541   /* it failed too...  can't find root= */   /* it failed too...  can't find root= */
1542          return 0;          return 0;
1543              }              }
# Line 1392  int suitableImage(struct singleEntry * e Line 1545  int suitableImage(struct singleEntry * e
1545      }      }
1546    
1547      dev = getpathbyspec(dev);      dev = getpathbyspec(dev);
1548      if (!dev)      if (!getpathbyspec(dev)) {
1549            notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev);
1550          return 0;          return 0;
1551        } else
1552     dev = getpathbyspec(dev);
1553    
1554      rootdev = findDiskForRoot();      rootdev = findDiskForRoot();
1555      if (!rootdev)      if (!rootdev) {
1556            notSuitablePrintf(entry, "can't find root device\n");
1557   return 0;   return 0;
1558        }
1559    
1560      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {      if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) {
1561            notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n",
1562     getuuidbydev(rootdev), getuuidbydev(dev));
1563          free(rootdev);          free(rootdev);
1564          return 0;          return 0;
1565      }      }
1566    
1567      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {      if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) {
1568            notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n",
1569     getuuidbydev(rootdev), getuuidbydev(dev));
1570   free(rootdev);   free(rootdev);
1571          return 0;          return 0;
1572      }      }
# Line 1632  void setDefaultImage(struct grubConfig * Line 1794  void setDefaultImage(struct grubConfig *
1794    
1795      /* 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
1796         changes */         changes */
1797      if (config->defaultImage == DEFAULT_SAVED)      if ((config->defaultImage == DEFAULT_SAVED) ||
1798     (config->defaultImage == DEFAULT_SAVED_GRUB2))
1799        /* default is set to saved, we don't want to change it */        /* default is set to saved, we don't want to change it */
1800        return;        return;
1801    
# Line 1699  void displayEntry(struct singleEntry * e Line 1862  void displayEntry(struct singleEntry * e
1862          return;          return;
1863      }      }
1864    
1865      printf("kernel=%s\n", line->elements[1].item);      printf("kernel=%s%s\n", prefix, line->elements[1].item);
1866    
1867      if (line->numElements >= 3) {      if (line->numElements >= 3) {
1868   printf("args=\"");   printf("args=\"");
# Line 3193  int main(int argc, const char ** argv) { Line 3356  int main(int argc, const char ** argv) {
3356        "the kernel referenced by the default image does not exist, "        "the kernel referenced by the default image does not exist, "
3357        "the first linux entry whose kernel does exist is used as the "        "the first linux entry whose kernel does exist is used as the "
3358        "template"), NULL },        "template"), NULL },
3359     { "debug", 0, 0, &debug, 0,
3360        _("print debugging information for failures") },
3361   { "default-kernel", 0, 0, &displayDefault, 0,   { "default-kernel", 0, 0, &displayDefault, 0,
3362      _("display the path of the default kernel") },      _("display the path of the default kernel") },
3363   { "default-index", 0, 0, &displayDefaultIndex, 0,   { "default-index", 0, 0, &displayDefaultIndex, 0,
# Line 3503  int main(int argc, const char ** argv) { Line 3668  int main(int argc, const char ** argv) {
3668    printf("%s\n", line->elements[1].item);    printf("%s\n", line->elements[1].item);
3669    
3670   } else {   } else {
3671    int i;    char * title;
   size_t len;  
   char * start;  
   char * tmp;  
3672    
3673    dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");    dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
3674    line = getLineByType(LT_MENUENTRY, entry->lines);    line = getLineByType(LT_MENUENTRY, entry->lines);
3675    if (!line) return 0;    if (!line) return 0;
3676      title = grub2ExtractTitle(line);
3677    for (i = 0; i < line->numElements; i++) {    if (title)
3678        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");  
3679   }   }
3680   return 0;   return 0;
3681    

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