Magellan Linux

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

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

revision 2246 by niro, Mon Oct 21 13:53:05 2013 UTC revision 2258 by niro, Mon Oct 21 14:02:25 2013 UTC
# Line 136  struct configFileInfo; Line 136  struct configFileInfo;
136  typedef const char *(*findConfigFunc)(struct configFileInfo *);  typedef const char *(*findConfigFunc)(struct configFileInfo *);
137  typedef const int (*writeLineFunc)(struct configFileInfo *,  typedef const int (*writeLineFunc)(struct configFileInfo *,
138   struct singleLine *line);   struct singleLine *line);
139    typedef char *(*getEnvFunc)(struct configFileInfo *, char *name);
140    typedef int (*setEnvFunc)(struct configFileInfo *, char *name, char *value);
141    
142  struct configFileInfo {  struct configFileInfo {
143      char * defaultConfig;      char * defaultConfig;
144      findConfigFunc findConfig;      findConfigFunc findConfig;
145      writeLineFunc writeLine;      writeLineFunc writeLine;
146        getEnvFunc getEnv;
147        setEnvFunc setEnv;
148      struct keywordTypes * keywords;      struct keywordTypes * keywords;
149      int caseInsensitive;      int caseInsensitive;
150      int defaultIsIndex;      int defaultIsIndex;
151      int defaultIsVariable;      int defaultIsVariable;
152      int defaultSupportSaved;      int defaultSupportSaved;
153        int defaultIsSaved;
154      enum lineType_e entryStart;      enum lineType_e entryStart;
155      enum lineType_e entryEnd;      enum lineType_e entryEnd;
156      int needsBootPrefix;      int needsBootPrefix;
# Line 157  struct configFileInfo { Line 162  struct configFileInfo {
162      int mbInitRdIsModule;      int mbInitRdIsModule;
163      int mbConcatArgs;      int mbConcatArgs;
164      int mbAllowExtraInitRds;      int mbAllowExtraInitRds;
165        char *envFile;
166  };  };
167    
168  struct keywordTypes grubKeywords[] = {  struct keywordTypes grubKeywords[] = {
# Line 261  const char *grub2FindConfig(struct confi Line 267  const char *grub2FindConfig(struct confi
267      return configFiles[i];      return configFiles[i];
268  }  }
269    
270    /* kind of hacky.  It'll give the first 1024 bytes, ish. */
271    static char *grub2GetEnv(struct configFileInfo *info, char *name)
272    {
273        static char buf[1025];
274        char *s = NULL;
275        char *ret = NULL;
276        char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv";
277        int rc = asprintf(&s, "grub2-editenv %s list | grep '^%s='", envFile, name);
278    
279        if (rc < 0)
280     return NULL;
281    
282        FILE *f = popen(s, "r");
283        if (!f)
284     goto out;
285    
286        memset(buf, '\0', sizeof (buf));
287        ret = fgets(buf, 1024, f);
288        pclose(f);
289    
290        if (ret) {
291     ret += strlen(name) + 1;
292     ret[strlen(ret) - 1] = '\0';
293        }
294        dbgPrintf("grub2GetEnv(%s): %s\n", name, ret);
295    out:
296        free(s);
297        return ret;
298    }
299    
300    static int sPopCount(const char *s, const char *c)
301    {
302        int ret = 0;
303        if (!s)
304     return -1;
305        for (int i = 0; s[i] != '\0'; i++)
306     for (int j = 0; c[j] != '\0'; j++)
307        if (s[i] == c[j])
308     ret++;
309        return ret;
310    }
311    
312    static char *shellEscape(const char *s)
313    {
314        int l = strlen(s) + sPopCount(s, "'") * 2;
315    
316        char *ret = calloc(l+1, sizeof (*ret));
317        if (!ret)
318     return NULL;
319        for (int i = 0, j = 0; s[i] != '\0'; i++, j++) {
320     if (s[i] == '\'')
321        ret[j++] = '\\';
322     ret[j] = s[i];
323        }
324        return ret;
325    }
326    
327    static void unquote(char *s)
328    {
329        int l = strlen(s);
330    
331        if ((s[l-1] == '\'' && s[0] == '\'') || (s[l-1] == '"' && s[0] == '"')) {
332     memmove(s, s+1, l-2);
333     s[l-2] = '\0';
334        }
335    }
336    
337    static int grub2SetEnv(struct configFileInfo *info, char *name, char *value)
338    {
339        char *s = NULL;
340        int rc = 0;
341        char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv";
342    
343        unquote(value);
344        value = shellEscape(value);
345        if (!value)
346        return -1;
347    
348        rc = asprintf(&s, "grub2-editenv %s set '%s=%s'", envFile, name, value);
349        free(value);
350        if (rc <0)
351     return -1;
352    
353        dbgPrintf("grub2SetEnv(%s): %s\n", name, s);
354        rc = system(s);
355        free(s);
356        return rc;
357    }
358    
359    /* this is a gigantic hack to avoid clobbering grub2 variables... */
360    static int is_special_grub2_variable(const char *name)
361    {
362        if (!strcmp(name,"\"${next_entry}\""))
363     return 1;
364        if (!strcmp(name,"\"${prev_saved_entry}\""))
365     return 1;
366        return 0;
367    }
368    
369  int sizeOfSingleLine(struct singleLine * line) {  int sizeOfSingleLine(struct singleLine * line) {
370    int count = 0;    int count = 0;
371    
# Line 355  char *grub2ExtractTitle(struct singleLin Line 460  char *grub2ExtractTitle(struct singleLin
460    
461  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
462      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
463        .getEnv = grub2GetEnv,
464        .setEnv = grub2SetEnv,
465      .keywords = grub2Keywords,      .keywords = grub2Keywords,
466      .defaultIsIndex = 1,      .defaultIsIndex = 1,
467      .defaultSupportSaved = 1,      .defaultSupportSaved = 1,
# Line 534  struct singleEntry * findEntryByIndex(st Line 641  struct singleEntry * findEntryByIndex(st
641  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
642       const char * path, const char * prefix,       const char * path, const char * prefix,
643       int * index);       int * index);
644    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
645          int * index);
646  static int readFile(int fd, char ** bufPtr);  static int readFile(int fd, char ** bufPtr);
647  static void lineInit(struct singleLine * line);  static void lineInit(struct singleLine * line);
648  struct singleLine * lineDup(struct singleLine * line);  struct singleLine * lineDup(struct singleLine * line);
# Line 691  static int isEntryStart(struct singleLin Line 800  static int isEntryStart(struct singleLin
800  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
801  static char * extractTitle(struct singleLine * line) {  static char * extractTitle(struct singleLine * line) {
802      /* bracketed title... let's extract it (leaks a byte) */      /* bracketed title... let's extract it (leaks a byte) */
803      char * title;      char * title = NULL;
804      title = strdup(line->elements[0].item);      if (line->type == LT_TITLE) {
805      title++;   title = strdup(line->elements[0].item);
806      *(title + strlen(title) - 1) = '\0';   title++;
807     *(title + strlen(title) - 1) = '\0';
808        } else if (line->type == LT_MENUENTRY)
809     title = strdup(line->elements[1].item);
810        else
811     return NULL;
812      return title;      return title;
813  }  }
814    
# Line 952  static int getNextLine(char ** bufPtr, s Line 1066  static int getNextLine(char ** bufPtr, s
1066      return 0;      return 0;
1067  }  }
1068    
1069    static int isnumber(const char *s)
1070    {
1071        int i;
1072        for (i = 0; s[i] != '\0'; i++)
1073     if (s[i] < '0' || s[i] > '9')
1074        return 0;
1075        return i;
1076    }
1077    
1078  static struct grubConfig * readConfig(const char * inName,  static struct grubConfig * readConfig(const char * inName,
1079        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
1080      int in;      int in;
# Line 1036  static struct grubConfig * readConfig(co Line 1159  static struct grubConfig * readConfig(co
1159      dbgPrintf("\n");      dbgPrintf("\n");
1160      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
1161      if (kwType && line->numElements == 3 &&      if (kwType && line->numElements == 3 &&
1162      !strcmp(line->elements[1].item, kwType->key)) {      !strcmp(line->elements[1].item, kwType->key) &&
1163        !is_special_grub2_variable(line->elements[2].item)) {
1164   dbgPrintf("Line sets default config\n");   dbgPrintf("Line sets default config\n");
1165   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1166   defaultLine = line;   defaultLine = line;
# Line 1241  static struct grubConfig * readConfig(co Line 1365  static struct grubConfig * readConfig(co
1365          if (defaultLine->numElements > 2 &&          if (defaultLine->numElements > 2 &&
1366      cfi->defaultSupportSaved &&      cfi->defaultSupportSaved &&
1367      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1368      cfg->defaultImage = DEFAULT_SAVED_GRUB2;   cfg->cfi->defaultIsSaved = 1;
1369     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1370     if (cfg->cfi->getEnv) {
1371        char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1372        if (defTitle) {
1373     int index = 0;
1374     if (isnumber(defTitle)) {
1375        index = atoi(defTitle);
1376        entry = findEntryByIndex(cfg, index);
1377     } else {
1378        entry = findEntryByTitle(cfg, defTitle, &index);
1379     }
1380     if (entry)
1381        cfg->defaultImage = index;
1382        }
1383     }
1384   } else if (cfi->defaultIsVariable) {   } else if (cfi->defaultIsVariable) {
1385      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1386      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
# Line 1282  static struct grubConfig * readConfig(co Line 1421  static struct grubConfig * readConfig(co
1421          cfg->defaultImage = -1;          cfg->defaultImage = -1;
1422      }      }
1423   }   }
1424        } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1425     char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1426     if (defTitle) {
1427        int index = 0;
1428        if (isnumber(defTitle)) {
1429     index = atoi(defTitle);
1430     entry = findEntryByIndex(cfg, index);
1431        } else {
1432     entry = findEntryByTitle(cfg, defTitle, &index);
1433        }
1434        if (entry)
1435     cfg->defaultImage = index;
1436     }
1437      } else {      } else {
1438          cfg->defaultImage = 0;          cfg->defaultImage = 0;
1439      }      }
# Line 1299  static void writeDefault(FILE * out, cha Line 1451  static void writeDefault(FILE * out, cha
1451    
1452      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1453   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1454      else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)      else if (cfg->cfi->defaultIsSaved) {
1455   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1456      else if (cfg->defaultImage > -1) {   if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1457        char *title;
1458        entry = findEntryByIndex(cfg, cfg->defaultImage);
1459        line = getLineByType(LT_MENUENTRY, entry->lines);
1460        if (!line)
1461     line = getLineByType(LT_TITLE, entry->lines);
1462        if (line) {
1463     title = extractTitle(line);
1464     if (title)
1465        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1466        }
1467     }
1468        } else if (cfg->defaultImage > -1) {
1469   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1470      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
1471          fprintf(out, "%sset default=\"%d\"\n", indent,          fprintf(out, "%sset default=\"%d\"\n", indent,
# Line 1404  static int writeConfig(struct grubConfig Line 1568  static int writeConfig(struct grubConfig
1568      while (line) {      while (line) {
1569          if (line->type == LT_SET_VARIABLE && defaultKw &&          if (line->type == LT_SET_VARIABLE && defaultKw &&
1570   line->numElements == 3 &&   line->numElements == 3 &&
1571   !strcmp(line->elements[1].item, defaultKw->key)) {   !strcmp(line->elements[1].item, defaultKw->key) &&
1572     !is_special_grub2_variable(line->elements[2].item)) {
1573      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1574      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1575   } else if (line->type == LT_DEFAULT) {   } else if (line->type == LT_DEFAULT) {
# Line 1859  struct singleEntry * findEntryByPath(str Line 2024  struct singleEntry * findEntryByPath(str
2024      return entry;      return entry;
2025  }  }
2026    
2027    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
2028          int * index) {
2029        struct singleEntry * entry;
2030        struct singleLine * line;
2031        int i;
2032        char * newtitle;
2033    
2034        for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
2035     if (index && i < *index)
2036        continue;
2037     line = getLineByType(LT_TITLE, entry->lines);
2038     if (!line)
2039        line = getLineByType(LT_MENUENTRY, entry->lines);
2040     if (!line)
2041        continue;
2042     newtitle = grub2ExtractTitle(line);
2043     if (!newtitle)
2044        continue;
2045     if (!strcmp(title, newtitle))
2046        break;
2047        }
2048    
2049        if (!entry)
2050     return NULL;
2051    
2052        if (index)
2053     *index = i;
2054        return entry;
2055    }
2056    
2057  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {
2058      struct singleEntry * entry;      struct singleEntry * entry;
2059    
# Line 1881  struct singleEntry * findTemplate(struct Line 2076  struct singleEntry * findTemplate(struct
2076      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2077      int index;      int index;
2078    
2079      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2080     if (cfg->cfi->getEnv) {
2081        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2082        if (defTitle) {
2083     int index = 0;
2084     if (isnumber(defTitle)) {
2085        index = atoi(defTitle);
2086        entry = findEntryByIndex(cfg, index);
2087     } else {
2088        entry = findEntryByTitle(cfg, defTitle, &index);
2089     }
2090     if (entry)
2091        cfg->defaultImage = index;
2092        }
2093     }
2094        } else if (cfg->defaultImage > -1) {
2095   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2096   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2097      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 3835  int main(int argc, const char ** argv) { Line 4045  int main(int argc, const char ** argv) {
4045      char * removeArgs = NULL;      char * removeArgs = NULL;
4046      char * kernelInfo = NULL;      char * kernelInfo = NULL;
4047      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
4048        char * envPath = NULL;
4049      const char * chptr = NULL;      const char * chptr = NULL;
4050      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
4051      struct grubConfig * config;      struct grubConfig * config;
# Line 3886  int main(int argc, const char ** argv) { Line 4097  int main(int argc, const char ** argv) {
4097      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4098   { "efi", 0, POPT_ARG_NONE, &isEfi, 0,   { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4099      _("force grub2 stanzas to use efi") },      _("force grub2 stanzas to use efi") },
4100     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4101        _("path for environment data"),
4102        _("path") },
4103   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4104      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
4105   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
# Line 3999  int main(int argc, const char ** argv) { Line 4213  int main(int argc, const char ** argv) {
4213   return 1;   return 1;
4214      } else if (configureGrub2) {      } else if (configureGrub2) {
4215   cfi = &grub2ConfigType;   cfi = &grub2ConfigType;
4216     if (envPath)
4217        cfi->envFile = envPath;
4218      } else if (configureLilo) {      } else if (configureLilo) {
4219   cfi = &liloConfigType;   cfi = &liloConfigType;
4220      } else if (configureGrub) {      } else if (configureGrub) {
# Line 4213  int main(int argc, const char ** argv) { Line 4429  int main(int argc, const char ** argv) {
4429          char * rootspec;          char * rootspec;
4430    
4431   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4432     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4433     cfi->defaultIsSaved)
4434        config->defaultImage = 0;
4435   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4436   if (!entry) return 0;   if (!entry) return 0;
4437   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
# Line 4231  int main(int argc, const char ** argv) { Line 4450  int main(int argc, const char ** argv) {
4450   struct singleEntry * entry;   struct singleEntry * entry;
4451    
4452   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1) return 0;
4453     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4454     cfi->defaultIsSaved)
4455        config->defaultImage = 0;
4456   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4457   if (!entry) return 0;   if (!entry) return 0;
4458    
# Line 4253  int main(int argc, const char ** argv) { Line 4475  int main(int argc, const char ** argv) {
4475    
4476      } else if (displayDefaultIndex) {      } else if (displayDefaultIndex) {
4477          if (config->defaultImage == -1) return 0;          if (config->defaultImage == -1) return 0;
4478     if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4479     cfi->defaultIsSaved)
4480        config->defaultImage = 0;
4481          printf("%i\n", config->defaultImage);          printf("%i\n", config->defaultImage);
4482            return 0;
4483    
4484      } else if (kernelInfo)      } else if (kernelInfo)
4485   return displayInfo(config, kernelInfo, bootPrefix);   return displayInfo(config, kernelInfo, bootPrefix);

Legend:
Removed from v.2246  
changed lines
  Added in v.2258