Magellan Linux

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

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

revision 2250 by niro, Mon Oct 21 13:56:22 2013 UTC revision 2252 by niro, Mon Oct 21 13:59:08 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 grub2SetEnv(struct configFileInfo *info, char *name, char *value)
301    {
302        char *s = NULL;
303        int rc = 0;
304        char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv";
305    
306        rc = asprintf(&s, "grub2-editenv %s set '%s=%s'", envFile, name, value);
307        if (rc <0)
308     return -1;
309    
310        dbgPrintf("grub2SetEnv(%s): %s\n", name, s);
311        rc = system(s);
312        free(s);
313        return rc;
314    }
315    
316    /* this is a gigantic hack to avoid clobbering grub2 variables... */
317    static int is_special_grub2_variable(const char *name)
318    {
319        if (!strcmp(name,"\"${next_entry}\""))
320     return 1;
321        if (!strcmp(name,"\"${prev_saved_entry}\""))
322     return 1;
323        return 0;
324    }
325    
326  int sizeOfSingleLine(struct singleLine * line) {  int sizeOfSingleLine(struct singleLine * line) {
327    int count = 0;    int count = 0;
328    
# Line 355  char *grub2ExtractTitle(struct singleLin Line 417  char *grub2ExtractTitle(struct singleLin
417    
418  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
419      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
420        .getEnv = grub2GetEnv,
421        .setEnv = grub2SetEnv,
422      .keywords = grub2Keywords,      .keywords = grub2Keywords,
423      .defaultIsIndex = 1,      .defaultIsIndex = 1,
424      .defaultSupportSaved = 1,      .defaultSupportSaved = 1,
# Line 534  struct singleEntry * findEntryByIndex(st Line 598  struct singleEntry * findEntryByIndex(st
598  struct singleEntry * findEntryByPath(struct grubConfig * cfg,  struct singleEntry * findEntryByPath(struct grubConfig * cfg,
599       const char * path, const char * prefix,       const char * path, const char * prefix,
600       int * index);       int * index);
601    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
602          int * index);
603  static int readFile(int fd, char ** bufPtr);  static int readFile(int fd, char ** bufPtr);
604  static void lineInit(struct singleLine * line);  static void lineInit(struct singleLine * line);
605  struct singleLine * lineDup(struct singleLine * line);  struct singleLine * lineDup(struct singleLine * line);
# Line 691  static int isEntryStart(struct singleLin Line 757  static int isEntryStart(struct singleLin
757  /* extract the title from within brackets (for zipl) */  /* extract the title from within brackets (for zipl) */
758  static char * extractTitle(struct singleLine * line) {  static char * extractTitle(struct singleLine * line) {
759      /* bracketed title... let's extract it (leaks a byte) */      /* bracketed title... let's extract it (leaks a byte) */
760      char * title;      char * title = NULL;
761      title = strdup(line->elements[0].item);      if (line->type == LT_TITLE) {
762      title++;   title = strdup(line->elements[0].item);
763      *(title + strlen(title) - 1) = '\0';   title++;
764     *(title + strlen(title) - 1) = '\0';
765        } else if (line->type == LT_MENUENTRY)
766     title = strdup(line->elements[1].item);
767        else
768     return NULL;
769      return title;      return title;
770  }  }
771    
# Line 1036  static struct grubConfig * readConfig(co Line 1107  static struct grubConfig * readConfig(co
1107      dbgPrintf("\n");      dbgPrintf("\n");
1108      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
1109      if (kwType && line->numElements == 3 &&      if (kwType && line->numElements == 3 &&
1110      !strcmp(line->elements[1].item, kwType->key)) {      !strcmp(line->elements[1].item, kwType->key) &&
1111        !is_special_grub2_variable(line->elements[2].item)) {
1112   dbgPrintf("Line sets default config\n");   dbgPrintf("Line sets default config\n");
1113   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1114   defaultLine = line;   defaultLine = line;
# Line 1241  static struct grubConfig * readConfig(co Line 1313  static struct grubConfig * readConfig(co
1313          if (defaultLine->numElements > 2 &&          if (defaultLine->numElements > 2 &&
1314      cfi->defaultSupportSaved &&      cfi->defaultSupportSaved &&
1315      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {      !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1316      cfg->defaultImage = DEFAULT_SAVED_GRUB2;   cfg->cfi->defaultIsSaved = 1;
1317     cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1318     if (cfg->cfi->getEnv) {
1319        char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1320        if (defTitle) {
1321     int index = 0;
1322     entry = findEntryByTitle(cfg, defTitle, &index);
1323     if (entry)
1324        cfg->defaultImage = index;
1325        }
1326     }
1327   } else if (cfi->defaultIsVariable) {   } else if (cfi->defaultIsVariable) {
1328      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1329      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
# Line 1282  static struct grubConfig * readConfig(co Line 1364  static struct grubConfig * readConfig(co
1364          cfg->defaultImage = -1;          cfg->defaultImage = -1;
1365      }      }
1366   }   }
1367        } else if (cfg->cfi->defaultIsSaved && cfg->cfi->getEnv) {
1368     char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1369     if (defTitle) {
1370        int index = 0;
1371        entry = findEntryByTitle(cfg, defTitle, &index);
1372        if (entry)
1373     cfg->defaultImage = index;
1374     }
1375      } else {      } else {
1376          cfg->defaultImage = 0;          cfg->defaultImage = 0;
1377      }      }
# Line 1299  static void writeDefault(FILE * out, cha Line 1389  static void writeDefault(FILE * out, cha
1389    
1390      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1391   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1392      else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)      else if (cfg->cfi->defaultIsSaved) {
1393   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);   fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1394      else if (cfg->defaultImage > -1) {   if (cfg->defaultImage >= 0 && cfg->cfi->setEnv) {
1395        char *title;
1396        entry = findEntryByIndex(cfg, cfg->defaultImage);
1397        line = getLineByType(LT_MENUENTRY, entry->lines);
1398        if (!line)
1399     line = getLineByType(LT_TITLE, entry->lines);
1400        if (line) {
1401     title = extractTitle(line);
1402     if (title)
1403        cfg->cfi->setEnv(cfg->cfi, "saved_entry", title);
1404        }
1405     }
1406        } else if (cfg->defaultImage > -1) {
1407   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1408      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
1409          fprintf(out, "%sset default=\"%d\"\n", indent,          fprintf(out, "%sset default=\"%d\"\n", indent,
# Line 1404  static int writeConfig(struct grubConfig Line 1506  static int writeConfig(struct grubConfig
1506      while (line) {      while (line) {
1507          if (line->type == LT_SET_VARIABLE && defaultKw &&          if (line->type == LT_SET_VARIABLE && defaultKw &&
1508   line->numElements == 3 &&   line->numElements == 3 &&
1509   !strcmp(line->elements[1].item, defaultKw->key)) {   !strcmp(line->elements[1].item, defaultKw->key) &&
1510     !is_special_grub2_variable(line->elements[2].item)) {
1511      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1512      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1513   } else if (line->type == LT_DEFAULT) {   } else if (line->type == LT_DEFAULT) {
# Line 1859  struct singleEntry * findEntryByPath(str Line 1962  struct singleEntry * findEntryByPath(str
1962      return entry;      return entry;
1963  }  }
1964    
1965    struct singleEntry * findEntryByTitle(struct grubConfig * cfg, char *title,
1966          int * index) {
1967        struct singleEntry * entry;
1968        struct singleLine * line;
1969        int i;
1970        char * newtitle;
1971    
1972        for (i = 0, entry = cfg->entries; entry; entry = entry->next, i++) {
1973     if (index && i < *index)
1974        continue;
1975     line = getLineByType(LT_TITLE, entry->lines);
1976     if (!line)
1977        line = getLineByType(LT_MENUENTRY, entry->lines);
1978     if (!line)
1979        continue;
1980     newtitle = grub2ExtractTitle(line);
1981     if (!newtitle)
1982        continue;
1983     if (!strcmp(title, newtitle))
1984        break;
1985        }
1986    
1987        if (!entry)
1988     return NULL;
1989    
1990        if (index)
1991     *index = i;
1992        return entry;
1993    }
1994    
1995  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {  struct singleEntry * findEntryByIndex(struct grubConfig * cfg, int index) {
1996      struct singleEntry * entry;      struct singleEntry * entry;
1997    
# Line 1881  struct singleEntry * findTemplate(struct Line 2014  struct singleEntry * findTemplate(struct
2014      struct singleEntry * entry, * entry2;      struct singleEntry * entry, * entry2;
2015      int index;      int index;
2016    
2017      if (cfg->defaultImage > -1) {      if (cfg->cfi->defaultIsSaved) {
2018     if (cfg->cfi->getEnv) {
2019        char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2020        if (defTitle) {
2021     int index = 0;
2022     entry = findEntryByTitle(cfg, defTitle, &index);
2023        }
2024     }
2025        } else if (cfg->defaultImage > -1) {
2026   entry = findEntryByIndex(cfg, cfg->defaultImage);   entry = findEntryByIndex(cfg, cfg->defaultImage);
2027   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {   if (entry && suitableImage(entry, prefix, skipRemoved, flags)) {
2028      if (indexPtr) *indexPtr = cfg->defaultImage;      if (indexPtr) *indexPtr = cfg->defaultImage;
# Line 3835  int main(int argc, const char ** argv) { Line 3976  int main(int argc, const char ** argv) {
3976      char * removeArgs = NULL;      char * removeArgs = NULL;
3977      char * kernelInfo = NULL;      char * kernelInfo = NULL;
3978      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };      char * extraInitrds[MAX_EXTRA_INITRDS] = { NULL };
3979        char * envPath = NULL;
3980      const char * chptr = NULL;      const char * chptr = NULL;
3981      struct configFileInfo * cfi = NULL;      struct configFileInfo * cfi = NULL;
3982      struct grubConfig * config;      struct grubConfig * config;
# Line 3886  int main(int argc, const char ** argv) { Line 4028  int main(int argc, const char ** argv) {
4028      _("configure elilo bootloader") },      _("configure elilo bootloader") },
4029   { "efi", 0, POPT_ARG_NONE, &isEfi, 0,   { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
4030      _("force grub2 stanzas to use efi") },      _("force grub2 stanzas to use efi") },
4031     { "env", 0, POPT_ARG_STRING, &envPath, 0,
4032        _("path for environment data"),
4033        _("path") },
4034   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
4035      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
4036   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
# Line 3999  int main(int argc, const char ** argv) { Line 4144  int main(int argc, const char ** argv) {
4144   return 1;   return 1;
4145      } else if (configureGrub2) {      } else if (configureGrub2) {
4146   cfi = &grub2ConfigType;   cfi = &grub2ConfigType;
4147     if (envPath)
4148        cfi->envFile = envPath;
4149      } else if (configureLilo) {      } else if (configureLilo) {
4150   cfi = &liloConfigType;   cfi = &liloConfigType;
4151      } else if (configureGrub) {      } else if (configureGrub) {

Legend:
Removed from v.2250  
changed lines
  Added in v.2252