Magellan Linux

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

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

revision 2257 by niro, Mon Oct 21 14:01:48 2013 UTC revision 2683 by niro, Wed Jul 16 09:51:14 2014 UTC
# Line 90  enum lineType_e { Line 90  enum lineType_e {
90      LT_SET_VARIABLE = 1 << 19,      LT_SET_VARIABLE = 1 << 19,
91      LT_KERNEL_EFI   = 1 << 20,      LT_KERNEL_EFI   = 1 << 20,
92      LT_INITRD_EFI   = 1 << 21,      LT_INITRD_EFI   = 1 << 21,
93      LT_UNKNOWN      = 1 << 22,      LT_KERNEL_16    = 1 << 22,
94        LT_INITRD_16    = 1 << 23,
95        LT_UNKNOWN      = 1 << 24,
96  };  };
97    
98  struct singleLine {  struct singleLine {
# Line 182  const char *grubFindConfig(struct config Line 184  const char *grubFindConfig(struct config
184   "/boot/grub/grub.conf",   "/boot/grub/grub.conf",
185   "/boot/grub/menu.lst",   "/boot/grub/menu.lst",
186   "/etc/grub.conf",   "/etc/grub.conf",
187     "/boot/grub2/grub.cfg",
188     "/boot/grub2-efi/grub.cfg",
189   NULL   NULL
190      };      };
191      static int i = -1;      static int i = -1;
# Line 221  struct keywordTypes grub2Keywords[] = { Line 225  struct keywordTypes grub2Keywords[] = {
225      { "fallback",   LT_FALLBACK,    ' ' },      { "fallback",   LT_FALLBACK,    ' ' },
226      { "linux",      LT_KERNEL,      ' ' },      { "linux",      LT_KERNEL,      ' ' },
227      { "linuxefi",   LT_KERNEL_EFI,  ' ' },      { "linuxefi",   LT_KERNEL_EFI,  ' ' },
228        { "linux16",    LT_KERNEL_16,   ' ' },
229      { "initrd",     LT_INITRD,      ' ', ' ' },      { "initrd",     LT_INITRD,      ' ', ' ' },
230      { "initrdefi",  LT_INITRD_EFI,  ' ', ' ' },      { "initrdefi",  LT_INITRD_EFI,  ' ', ' ' },
231        { "initrd16",   LT_INITRD_16,   ' ', ' ' },
232      { "module",     LT_MBMODULE,    ' ' },      { "module",     LT_MBMODULE,    ' ' },
233      { "kernel",     LT_HYPER,       ' ' },      { "kernel",     LT_HYPER,       ' ' },
234      { NULL, 0, 0 },      { NULL, 0, 0 },
# Line 273  static char *grub2GetEnv(struct configFi Line 279  static char *grub2GetEnv(struct configFi
279      static char buf[1025];      static char buf[1025];
280      char *s = NULL;      char *s = NULL;
281      char *ret = NULL;      char *ret = NULL;
282      char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv";      char *envFile = info->envFile ? info->envFile : "/boot/grub/grubenv";
283      int rc = asprintf(&s, "grub2-editenv %s list | grep '^%s='", envFile, name);      int rc = asprintf(&s, "grub-editenv %s list | grep '^%s='", envFile, name);
284    
285      if (rc < 0)      if (rc < 0)
286   return NULL;   return NULL;
# Line 338  static int grub2SetEnv(struct configFile Line 344  static int grub2SetEnv(struct configFile
344  {  {
345      char *s = NULL;      char *s = NULL;
346      int rc = 0;      int rc = 0;
347      char *envFile = info->envFile ? info->envFile : "/boot/grub2/grubenv";      char *envFile = info->envFile ? info->envFile : "/boot/grub/grubenv";
348    
349      unquote(value);      unquote(value);
350      value = shellEscape(value);      value = shellEscape(value);
351      if (!value)      if (!value)
352      return -1;      return -1;
353    
354      rc = asprintf(&s, "grub2-editenv %s set '%s=%s'", envFile, name, value);      rc = asprintf(&s, "grub-editenv %s set '%s=%s'", envFile, name, value);
355      free(value);      free(value);
356      if (rc <0)      if (rc <0)
357   return -1;   return -1;
# Line 396  static int isquote(char q) Line 402  static int isquote(char q)
402  }  }
403    
404  static int iskernel(enum lineType_e type) {  static int iskernel(enum lineType_e type) {
405      return (type == LT_KERNEL || type == LT_KERNEL_EFI);      return (type == LT_KERNEL || type == LT_KERNEL_EFI || type == LT_KERNEL_16);
406  }  }
407    
408  static int isinitrd(enum lineType_e type) {  static int isinitrd(enum lineType_e type) {
409      return (type == LT_INITRD || type == LT_INITRD_EFI);      return (type == LT_INITRD || type == LT_INITRD_EFI || type == LT_INITRD_16);
410  }  }
411    
412  char *grub2ExtractTitle(struct singleLine * line) {  char *grub2ExtractTitle(struct singleLine * line) {
# Line 718  static enum lineType_e preferredLineType Line 724  static enum lineType_e preferredLineType
724   default:   default:
725      return type;      return type;
726   }   }
727    #if defined(__i386__) || defined(__x86_64__)
728        } else if (cfi == &grub2ConfigType) {
729     switch (type) {
730     case LT_KERNEL:
731        return LT_KERNEL_16;
732     case LT_INITRD:
733        return LT_INITRD_16;
734     default:
735        return type;
736     }
737    #endif
738      }      }
739      return type;      return type;
740  }  }
# Line 1066  static int getNextLine(char ** bufPtr, s Line 1083  static int getNextLine(char ** bufPtr, s
1083      return 0;      return 0;
1084  }  }
1085    
1086    static int isnumber(const char *s)
1087    {
1088        int i;
1089        for (i = 0; s[i] != '\0'; i++)
1090     if (s[i] < '0' || s[i] > '9')
1091        return 0;
1092        return i;
1093    }
1094    
1095  static struct grubConfig * readConfig(const char * inName,  static struct grubConfig * readConfig(const char * inName,
1096        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
1097      int in;      int in;
# Line 1362  static struct grubConfig * readConfig(co Line 1388  static struct grubConfig * readConfig(co
1388      char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");      char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1389      if (defTitle) {      if (defTitle) {
1390   int index = 0;   int index = 0;
1391   entry = findEntryByTitle(cfg, defTitle, &index);   if (isnumber(defTitle)) {
1392        index = atoi(defTitle);
1393        entry = findEntryByIndex(cfg, index);
1394     } else {
1395        entry = findEntryByTitle(cfg, defTitle, &index);
1396     }
1397   if (entry)   if (entry)
1398      cfg->defaultImage = index;      cfg->defaultImage = index;
1399      }      }
# Line 1411  static struct grubConfig * readConfig(co Line 1442  static struct grubConfig * readConfig(co
1442   char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");   char *defTitle = cfi->getEnv(cfg->cfi, "saved_entry");
1443   if (defTitle) {   if (defTitle) {
1444      int index = 0;      int index = 0;
1445      entry = findEntryByTitle(cfg, defTitle, &index);      if (isnumber(defTitle)) {
1446     index = atoi(defTitle);
1447     entry = findEntryByIndex(cfg, index);
1448        } else {
1449     entry = findEntryByTitle(cfg, defTitle, &index);
1450        }
1451      if (entry)      if (entry)
1452   cfg->defaultImage = index;   cfg->defaultImage = index;
1453   }   }
# Line 1786  int suitableImage(struct singleEntry * e Line 1822  int suitableImage(struct singleEntry * e
1822   return 0;   return 0;
1823      }      }
1824    
1825      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
1826      if (!line) {      if (!line) {
1827   notSuitablePrintf(entry, 0, "no line found\n");   notSuitablePrintf(entry, 0, "no line found\n");
1828   return 0;   return 0;
# Line 1920  struct singleEntry * findEntryByPath(str Line 1956  struct singleEntry * findEntryByPath(str
1956   entry = findEntryByIndex(config, indexVars[i]);   entry = findEntryByIndex(config, indexVars[i]);
1957   if (!entry) return NULL;   if (!entry) return NULL;
1958    
1959   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
1960   if (!line) return NULL;   if (!line) return NULL;
1961    
1962   if (index) *index = indexVars[i];   if (index) *index = indexVars[i];
# Line 1971  struct singleEntry * findEntryByPath(str Line 2007  struct singleEntry * findEntryByPath(str
2007      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
2008   enum lineType_e ct = checkType;   enum lineType_e ct = checkType;
2009   if (entry->multiboot && checkType == LT_KERNEL)   if (entry->multiboot && checkType == LT_KERNEL)
2010      ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER;      ct = LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER|LT_KERNEL_16;
2011   else if (checkType & LT_KERNEL)   else if (checkType & LT_KERNEL)
2012      ct = checkType | LT_KERNEL_EFI;      ct = checkType | LT_KERNEL_EFI | LT_KERNEL_16;
2013   line = getLineByType(ct, line);   line = getLineByType(ct, line);
2014   if (!line)   if (!line)
2015      break;  /* not found in this entry */      break;  /* not found in this entry */
# Line 1995  struct singleEntry * findEntryByPath(str Line 2031  struct singleEntry * findEntryByPath(str
2031       * non-Linux boot entries (could find netbsd etc, though, which is       * non-Linux boot entries (could find netbsd etc, though, which is
2032       * unfortunate)       * unfortunate)
2033       */       */
2034      if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines))      if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines))
2035   break; /* found 'im! */   break; /* found 'im! */
2036   }   }
2037    
# Line 2062  struct singleEntry * findTemplate(struct Line 2098  struct singleEntry * findTemplate(struct
2098      char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");      char *defTitle = cfg->cfi->getEnv(cfg->cfi, "saved_entry");
2099      if (defTitle) {      if (defTitle) {
2100   int index = 0;   int index = 0;
2101   entry = findEntryByTitle(cfg, defTitle, &index);   if (isnumber(defTitle)) {
2102        index = atoi(defTitle);
2103        entry = findEntryByIndex(cfg, index);
2104     } else {
2105        entry = findEntryByTitle(cfg, defTitle, &index);
2106     }
2107     if (entry)
2108        cfg->defaultImage = index;
2109      }      }
2110   }   }
2111      } else if (cfg->defaultImage > -1) {      } else if (cfg->defaultImage > -1) {
# Line 2222  void displayEntry(struct singleEntry * e Line 2265  void displayEntry(struct singleEntry * e
2265    
2266      printf("index=%d\n", index);      printf("index=%d\n", index);
2267    
2268      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
2269      if (!line) {      if (!line) {
2270          printf("non linux entry\n");          printf("non linux entry\n");
2271          return;          return;
# Line 2287  void displayEntry(struct singleEntry * e Line 2330  void displayEntry(struct singleEntry * e
2330   printf("root=%s\n", s);   printf("root=%s\n", s);
2331      }      }
2332    
2333      line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);      line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
2334    
2335      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2336   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
# Line 2704  struct singleLine * addLineTmpl(struct s Line 2747  struct singleLine * addLineTmpl(struct s
2747   insertElement(newLine, val, 1, cfi);   insertElement(newLine, val, 1, cfi);
2748    
2749   /* but try to keep the rootspec from the template... sigh */   /* but try to keep the rootspec from the template... sigh */
2750   if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD|LT_KERNEL_EFI|LT_INITRD_EFI)) {   if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD|LT_KERNEL_EFI|LT_INITRD_EFI|LT_KERNEL_16|LT_INITRD_16)) {
2751      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);
2752      if (rootspec != NULL) {      if (rootspec != NULL) {
2753   free(newLine->elements[1].item);   free(newLine->elements[1].item);
# Line 3074  int updateActualImage(struct grubConfig Line 3117  int updateActualImage(struct grubConfig
3117      firstElement = 2;      firstElement = 2;
3118    
3119   } else {   } else {
3120      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI, entry->lines);      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3121      if (!line) {      if (!line) {
3122   /* no LT_KERNEL or LT_MBMODULE in this entry? */   /* no LT_KERNEL or LT_MBMODULE in this entry? */
3123   continue;   continue;
# Line 3230  int updateImage(struct grubConfig * cfg, Line 3273  int updateImage(struct grubConfig * cfg,
3273      return rc;      return rc;
3274  }  }
3275    
3276    int addMBInitrd(struct grubConfig * cfg, const char *newMBKernel,
3277     const char * image, const char * prefix, const char * initrd) {
3278        struct singleEntry * entry;
3279        struct singleLine * line, * kernelLine, *endLine = NULL;
3280        int index = 0;
3281    
3282        if (!image) return 0;
3283    
3284        for (; (entry = findEntryByPath(cfg, newMBKernel, prefix, &index)); index++) {
3285            kernelLine = getLineByType(LT_MBMODULE, entry->lines);
3286            if (!kernelLine) continue;
3287    
3288            if (prefix) {
3289                int prefixLen = strlen(prefix);
3290                if (!strncmp(initrd, prefix, prefixLen))
3291                    initrd += prefixLen;
3292            }
3293     endLine = getLineByType(LT_ENTRY_END, entry->lines);
3294     if (endLine)
3295        removeLine(entry, endLine);
3296            line = addLine(entry, cfg->cfi, preferredLineType(LT_MBMODULE,cfg->cfi),
3297     kernelLine->indent, initrd);
3298            if (!line)
3299        return 1;
3300     if (endLine) {
3301        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
3302                if (!line)
3303     return 1;
3304     }
3305    
3306            break;
3307        }
3308    
3309        return 0;
3310    }
3311    
3312  int updateInitrd(struct grubConfig * cfg, const char * image,  int updateInitrd(struct grubConfig * cfg, const char * image,
3313                   const char * prefix, const char * initrd) {                   const char * prefix, const char * initrd) {
3314      struct singleEntry * entry;      struct singleEntry * entry;
# Line 3239  int updateInitrd(struct grubConfig * cfg Line 3318  int updateInitrd(struct grubConfig * cfg
3318      if (!image) return 0;      if (!image) return 0;
3319    
3320      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3321          kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI, entry->lines);          kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
3322          if (!kernelLine) continue;          if (!kernelLine) continue;
3323    
3324          line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);          line = getLineByType(LT_INITRD|LT_INITRD_EFI|LT_INITRD_16, entry->lines);
3325          if (line)          if (line)
3326              removeLine(entry, line);              removeLine(entry, line);
3327          if (prefix) {          if (prefix) {
# Line 3253  int updateInitrd(struct grubConfig * cfg Line 3332  int updateInitrd(struct grubConfig * cfg
3332   endLine = getLineByType(LT_ENTRY_END, entry->lines);   endLine = getLineByType(LT_ENTRY_END, entry->lines);
3333   if (endLine)   if (endLine)
3334      removeLine(entry, endLine);      removeLine(entry, endLine);
3335          line = addLine(entry, cfg->cfi, preferredLineType(LT_INITRD, cfg->cfi),   enum lineType_e lt;
3336   kernelLine->indent, initrd);   switch(kernelLine->type) {
3337        case LT_KERNEL:
3338            lt = LT_INITRD;
3339     break;
3340        case LT_KERNEL_EFI:
3341            lt = LT_INITRD_EFI;
3342     break;
3343        case LT_KERNEL_16:
3344            lt = LT_INITRD_16;
3345     break;
3346        default:
3347            lt = preferredLineType(LT_INITRD, cfg->cfi);
3348     }
3349            line = addLine(entry, cfg->cfi, lt, kernelLine->indent, initrd);
3350          if (!line)          if (!line)
3351      return 1;      return 1;
3352   if (endLine) {   if (endLine) {
# Line 3852  int addNewKernel(struct grubConfig * con Line 3944  int addNewKernel(struct grubConfig * con
3944   switch (config->cfi->entryStart) {   switch (config->cfi->entryStart) {
3945      case LT_KERNEL:      case LT_KERNEL:
3946      case LT_KERNEL_EFI:      case LT_KERNEL_EFI:
3947        case LT_KERNEL_16:
3948   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
3949      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
3950   } else {   } else {
# Line 4410  int main(int argc, const char ** argv) { Line 4503  int main(int argc, const char ** argv) {
4503   if (!entry) return 0;   if (!entry) return 0;
4504   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
4505    
4506   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI|LT_KERNEL_16, entry->lines);
4507   if (!line) return 0;   if (!line) return 0;
4508    
4509          rootspec = getRootSpecifier(line->elements[1].item);          rootspec = getRootSpecifier(line->elements[1].item);
# Line 4471  int main(int argc, const char ** argv) { Line 4564  int main(int argc, const char ** argv) {
4564      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,      if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs,
4565                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;                      removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1;
4566      if (updateKernelPath && newKernelInitrd) {      if (updateKernelPath && newKernelInitrd) {
4567              if (updateInitrd(config, updateKernelPath, bootPrefix,      if (newMBKernel) {
4568                               newKernelInitrd)) return 1;      if (addMBInitrd(config, newMBKernel, updateKernelPath,
4569     bootPrefix, newKernelInitrd))
4570        return 1;
4571        } else {
4572        if (updateInitrd(config, updateKernelPath, bootPrefix,
4573     newKernelInitrd))
4574     return 1;
4575        }
4576      }      }
4577      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
4578                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,

Legend:
Removed from v.2257  
changed lines
  Added in v.2683