Magellan Linux

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

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

revision 1934 by niro, Mon Oct 1 12:08:46 2012 UTC revision 1940 by niro, Mon Oct 1 12:39:50 2012 UTC
# Line 56  int debug = 0; /* Currently just for tem Line 56  int debug = 0; /* Currently just for tem
56  #define NOOP_OPCODE 0x90  #define NOOP_OPCODE 0x90
57  #define JMP_SHORT_OPCODE 0xeb  #define JMP_SHORT_OPCODE 0xeb
58    
59    int isEfi = 0;
60    
61  /* comments get lumped in with indention */  /* comments get lumped in with indention */
62  struct lineElement {  struct lineElement {
63      char * item;      char * item;
# Line 82  enum lineType_e { Line 84  enum lineType_e {
84      LT_MENUENTRY    = 1 << 17,      LT_MENUENTRY    = 1 << 17,
85      LT_ENTRY_END    = 1 << 18,      LT_ENTRY_END    = 1 << 18,
86      LT_SET_VARIABLE = 1 << 19,      LT_SET_VARIABLE = 1 << 19,
87      LT_UNKNOWN      = 1 << 20,      LT_KERNEL_EFI   = 1 << 20,
88        LT_INITRD_EFI   = 1 << 21,
89        LT_UNKNOWN      = 1 << 22,
90  };  };
91    
92  struct singleLine {  struct singleLine {
# Line 205  struct keywordTypes grub2Keywords[] = { Line 209  struct keywordTypes grub2Keywords[] = {
209      { "default",    LT_DEFAULT,     ' ' },      { "default",    LT_DEFAULT,     ' ' },
210      { "fallback",   LT_FALLBACK,    ' ' },      { "fallback",   LT_FALLBACK,    ' ' },
211      { "linux",      LT_KERNEL,      ' ' },      { "linux",      LT_KERNEL,      ' ' },
212      { "linuxefi",   LT_KERNEL,      ' ' },      { "linuxefi",   LT_KERNEL_EFI,  ' ' },
213      { "initrd",     LT_INITRD,      ' ', ' ' },      { "initrd",     LT_INITRD,      ' ', ' ' },
214      { "initrdefi",  LT_INITRD,      ' ', ' ' },      { "initrdefi",  LT_INITRD_EFI,  ' ', ' ' },
215      { "module",     LT_MBMODULE,    ' ' },      { "module",     LT_MBMODULE,    ' ' },
216      { "kernel",     LT_HYPER,       ' ' },      { "kernel",     LT_HYPER,       ' ' },
217      { NULL, 0, 0 },      { NULL, 0, 0 },
# Line 273  static int isquote(char q) Line 277  static int isquote(char q)
277      return 0;      return 0;
278  }  }
279    
280    static int iskernel(enum lineType_e type) {
281        return (type == LT_KERNEL || type == LT_KERNEL_EFI);
282    }
283    
284    static int isinitrd(enum lineType_e type) {
285        return (type == LT_INITRD || type == LT_INITRD_EFI);
286    }
287    
288  char *grub2ExtractTitle(struct singleLine * line) {  char *grub2ExtractTitle(struct singleLine * line) {
289      char * current;      char * current;
290      char * current_indent;      char * current_indent;
# Line 572  static char * sdupprintf(const char *for Line 584  static char * sdupprintf(const char *for
584      return buf;      return buf;
585  }  }
586    
587    static enum lineType_e preferredLineType(enum lineType_e type,
588     struct configFileInfo *cfi) {
589        if (isEfi && cfi == &grub2ConfigType) {
590     switch (type) {
591     case LT_KERNEL:
592        return LT_KERNEL_EFI;
593     case LT_INITRD:
594        return LT_INITRD_EFI;
595     default:
596        return type;
597     }
598        }
599        return type;
600    }
601    
602  static struct keywordTypes * getKeywordByType(enum lineType_e type,  static struct keywordTypes * getKeywordByType(enum lineType_e type,
603        struct configFileInfo * cfi) {        struct configFileInfo * cfi) {
604      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {      for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) {
# Line 996  static struct grubConfig * readConfig(co Line 1023  static struct grubConfig * readConfig(co
1023      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1024      defaultLine = line;      defaultLine = line;
1025    
1026          } else if (line->type == LT_KERNEL) {          } else if (iskernel(line->type)) {
1027      /* if by some freak chance this is multiboot and the "module"      /* if by some freak chance this is multiboot and the "module"
1028       * lines came earlier in the template, make sure to use LT_HYPER       * lines came earlier in the template, make sure to use LT_HYPER
1029       * instead of LT_KERNEL now       * instead of LT_KERNEL now
# Line 1013  static struct grubConfig * readConfig(co Line 1040  static struct grubConfig * readConfig(co
1040      for (struct singleLine *l = entry->lines; l; l = l->next) {      for (struct singleLine *l = entry->lines; l; l = l->next) {
1041   if (l->type == LT_HYPER)   if (l->type == LT_HYPER)
1042      break;      break;
1043   else if (l->type == LT_KERNEL) {   else if (iskernel(l->type)) {
1044      l->type = LT_HYPER;      l->type = LT_HYPER;
1045      break;      break;
1046   }   }
# Line 1567  int suitableImage(struct singleEntry * e Line 1594  int suitableImage(struct singleEntry * e
1594   return 0;   return 0;
1595      }      }
1596    
1597      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
1598      if (!line) {      if (!line) {
1599   notSuitablePrintf(entry, "no line found\n");   notSuitablePrintf(entry, "no line found\n");
1600   return 0;   return 0;
# Line 1697  struct singleEntry * findEntryByPath(str Line 1724  struct singleEntry * findEntryByPath(str
1724   entry = findEntryByIndex(config, indexVars[i]);   entry = findEntryByIndex(config, indexVars[i]);
1725   if (!entry) return NULL;   if (!entry) return NULL;
1726    
1727   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
1728   if (!line) return NULL;   if (!line) return NULL;
1729    
1730   if (index) *index = indexVars[i];   if (index) *index = indexVars[i];
# Line 1746  struct singleEntry * findEntryByPath(str Line 1773  struct singleEntry * findEntryByPath(str
1773    
1774      /* check all the lines matching checkType */      /* check all the lines matching checkType */
1775      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1776   line = getLineByType(entry->multiboot && checkType == LT_KERNEL ?   line = getLineByType(entry->multiboot && checkType == LT_KERNEL
1777       LT_KERNEL|LT_MBMODULE|LT_HYPER :   ? LT_KERNEL|LT_KERNEL_EFI|LT_MBMODULE|LT_HYPER
1778       checkType, line);   : checkType, line);
1779   if (!line) break;  /* not found in this entry */   if (!line) break;  /* not found in this entry */
1780    
1781   if (line && line->type != LT_MENUENTRY &&   if (line && line->type != LT_MENUENTRY &&
# Line 1768  struct singleEntry * findEntryByPath(str Line 1795  struct singleEntry * findEntryByPath(str
1795       * non-Linux boot entries (could find netbsd etc, though, which is       * non-Linux boot entries (could find netbsd etc, though, which is
1796       * unfortunate)       * unfortunate)
1797       */       */
1798      if (line && getLineByType(LT_KERNEL|LT_HYPER, entry->lines))      if (line && getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines))
1799   break; /* found 'im! */   break; /* found 'im! */
1800   }   }
1801    
# Line 1957  void displayEntry(struct singleEntry * e Line 1984  void displayEntry(struct singleEntry * e
1984    
1985      printf("index=%d\n", index);      printf("index=%d\n", index);
1986    
1987      line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);      line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
1988      if (!line) {      if (!line) {
1989          printf("non linux entry\n");          printf("non linux entry\n");
1990          return;          return;
# Line 2022  void displayEntry(struct singleEntry * e Line 2049  void displayEntry(struct singleEntry * e
2049   printf("root=%s\n", s);   printf("root=%s\n", s);
2050      }      }
2051    
2052      line = getLineByType(LT_INITRD, entry->lines);      line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);
2053    
2054      if (line && line->numElements >= 2) {      if (line && line->numElements >= 2) {
2055   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))   if (!strncmp(prefix, line->elements[1].item, strlen(prefix)))
# Line 2423  struct singleLine * addLineTmpl(struct s Line 2450  struct singleLine * addLineTmpl(struct s
2450  {  {
2451      struct singleLine * newLine = lineDup(tmplLine);      struct singleLine * newLine = lineDup(tmplLine);
2452    
2453        if (isEfi && cfi == &grub2ConfigType) {
2454     enum lineType_e old = newLine->type;
2455     newLine->type = preferredLineType(newLine->type, cfi);
2456     if (old != newLine->type)
2457        newLine->elements[0].item = getKeyByType(newLine->type, cfi);
2458        }
2459    
2460      if (val) {      if (val) {
2461   /* override the inherited value with our own.   /* override the inherited value with our own.
2462   * This is a little weak because it only applies to elements[1]   * This is a little weak because it only applies to elements[1]
# Line 2432  struct singleLine * addLineTmpl(struct s Line 2466  struct singleLine * addLineTmpl(struct s
2466   insertElement(newLine, val, 1, cfi);   insertElement(newLine, val, 1, cfi);
2467    
2468   /* but try to keep the rootspec from the template... sigh */   /* but try to keep the rootspec from the template... sigh */
2469   if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD)) {   if (tmplLine->type & (LT_HYPER|LT_KERNEL|LT_MBMODULE|LT_INITRD|LT_KERNEL_EFI|LT_INITRD_EFI)) {
2470      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);      char * rootspec = getRootSpecifier(tmplLine->elements[1].item);
2471      if (rootspec != NULL) {      if (rootspec != NULL) {
2472   free(newLine->elements[1].item);   free(newLine->elements[1].item);
# Line 2469  struct singleLine *  addLine(struct sing Line 2503  struct singleLine *  addLine(struct sing
2503      /* NB: This function shouldn't allocate items on the heap, rather on the      /* NB: This function shouldn't allocate items on the heap, rather on the
2504       * stack since it calls addLineTmpl which will make copies.       * stack since it calls addLineTmpl which will make copies.
2505       */       */
   
2506      if (type == LT_TITLE && cfi->titleBracketed) {      if (type == LT_TITLE && cfi->titleBracketed) {
2507   /* we're doing a bracketed title (zipl) */   /* we're doing a bracketed title (zipl) */
2508   tmpl.type = type;   tmpl.type = type;
# Line 2803  int updateActualImage(struct grubConfig Line 2836  int updateActualImage(struct grubConfig
2836      firstElement = 2;      firstElement = 2;
2837    
2838   } else {   } else {
2839      line = getLineByType(LT_KERNEL|LT_MBMODULE, entry->lines);      line = getLineByType(LT_KERNEL|LT_MBMODULE|LT_KERNEL_EFI, entry->lines);
2840      if (!line) {      if (!line) {
2841   /* no LT_KERNEL or LT_MBMODULE in this entry? */   /* no LT_KERNEL or LT_MBMODULE in this entry? */
2842   continue;   continue;
# Line 2968  int updateInitrd(struct grubConfig * cfg Line 3001  int updateInitrd(struct grubConfig * cfg
3001      if (!image) return 0;      if (!image) return 0;
3002    
3003      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {      for (; (entry = findEntryByPath(cfg, image, prefix, &index)); index++) {
3004          kernelLine = getLineByType(LT_KERNEL, entry->lines);          kernelLine = getLineByType(LT_KERNEL|LT_KERNEL_EFI, entry->lines);
3005          if (!kernelLine) continue;          if (!kernelLine) continue;
3006    
3007          line = getLineByType(LT_INITRD, entry->lines);          line = getLineByType(LT_INITRD|LT_INITRD_EFI, entry->lines);
3008          if (line)          if (line)
3009              removeLine(entry, line);              removeLine(entry, line);
3010          if (prefix) {          if (prefix) {
# Line 2982  int updateInitrd(struct grubConfig * cfg Line 3015  int updateInitrd(struct grubConfig * cfg
3015   endLine = getLineByType(LT_ENTRY_END, entry->lines);   endLine = getLineByType(LT_ENTRY_END, entry->lines);
3016   if (endLine)   if (endLine)
3017      removeLine(entry, endLine);      removeLine(entry, endLine);
3018          line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);          line = addLine(entry, cfg->cfi, preferredLineType(LT_INITRD, cfg->cfi),
3019     kernelLine->indent, initrd);
3020          if (!line)          if (!line)
3021      return 1;      return 1;
3022   if (endLine) {   if (endLine) {
# Line 3395  int addNewKernel(struct grubConfig * con Line 3429  int addNewKernel(struct grubConfig * con
3429      while (*chptr && isspace(*chptr)) chptr++;      while (*chptr && isspace(*chptr)) chptr++;
3430      if (*chptr == '#') continue;      if (*chptr == '#') continue;
3431    
3432      if (tmplLine->type == LT_KERNEL &&      if (iskernel(tmplLine->type) && tmplLine->numElements >= 2) {
     tmplLine->numElements >= 2) {  
3433   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
3434      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
3435       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 3473  int addNewKernel(struct grubConfig * con Line 3506  int addNewKernel(struct grubConfig * con
3506      /* template is multi but new is not,      /* template is multi but new is not,
3507       * insert the kernel in the first module slot       * insert the kernel in the first module slot
3508       */       */
3509      tmplLine->type = LT_KERNEL;      tmplLine->type = preferredLineType(LT_KERNEL, config->cfi);
3510      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3511      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3512   strdup(getKeywordByType(LT_KERNEL, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3513     config->cfi)->key);
3514      newLine = addLineTmpl(new, tmplLine, newLine,      newLine = addLineTmpl(new, tmplLine, newLine,
3515    newKernelPath + strlen(prefix), config->cfi);    newKernelPath + strlen(prefix),
3516      config->cfi);
3517      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
3518   } else if (needs & NEED_INITRD) {   } else if (needs & NEED_INITRD) {
3519      char *initrdVal;      char *initrdVal;
3520      /* template is multi but new is not,      /* template is multi but new is not,
3521       * insert the initrd in the second module slot       * insert the initrd in the second module slot
3522       */       */
3523      tmplLine->type = LT_INITRD;      tmplLine->type = preferredLineType(LT_INITRD, config->cfi);
3524      free(tmplLine->elements[0].item);      free(tmplLine->elements[0].item);
3525      tmplLine->elements[0].item =      tmplLine->elements[0].item =
3526   strdup(getKeywordByType(LT_INITRD, config->cfi)->key);   strdup(getKeywordByType(tmplLine->type,
3527     config->cfi)->key);
3528      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);      initrdVal = getInitrdVal(config, prefix, tmplLine, newKernelInitrd, extraInitrds, extraInitrdCount);
3529      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);      newLine = addLineTmpl(new, tmplLine, newLine, initrdVal, config->cfi);
3530      free(initrdVal);      free(initrdVal);
3531      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
3532   }   }
3533    
3534      } else if (tmplLine->type == LT_INITRD &&      } else if (isinitrd(tmplLine->type) && tmplLine->numElements >= 2) {
        tmplLine->numElements >= 2) {  
3535   if (needs & NEED_INITRD &&   if (needs & NEED_INITRD &&
3536      new->multiboot && !template->multiboot &&      new->multiboot && !template->multiboot &&
3537      config->cfi->mbInitRdIsModule) {      config->cfi->mbInitRdIsModule) {
# Line 3550  int addNewKernel(struct grubConfig * con Line 3585  int addNewKernel(struct grubConfig * con
3585      static const char *prefix = "'Loading ";      static const char *prefix = "'Loading ";
3586      if (tmplLine->numElements > 1 &&      if (tmplLine->numElements > 1 &&
3587      strstr(tmplLine->elements[1].item, prefix) &&      strstr(tmplLine->elements[1].item, prefix) &&
3588      masterLine->next && masterLine->next->type == LT_KERNEL) {      masterLine->next &&
3589        iskernel(masterLine->next->type)) {
3590   char *newTitle = malloc(strlen(prefix) +   char *newTitle = malloc(strlen(prefix) +
3591   strlen(newKernelTitle) + 2);   strlen(newKernelTitle) + 2);
3592    
# Line 3577  int addNewKernel(struct grubConfig * con Line 3613  int addNewKernel(struct grubConfig * con
3613   */   */
3614   switch (config->cfi->entryStart) {   switch (config->cfi->entryStart) {
3615      case LT_KERNEL:      case LT_KERNEL:
3616        case LT_KERNEL_EFI:
3617   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
3618      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
3619   } else {   } else {
3620      newLine = addLine(new, config->cfi, LT_KERNEL,      newLine = addLine(new, config->cfi,
3621              preferredLineType(LT_KERNEL, config->cfi),
3622        config->primaryIndent,        config->primaryIndent,
3623        newKernelPath + strlen(prefix));        newKernelPath + strlen(prefix));
3624      needs &= ~NEED_KERNEL;      needs &= ~NEED_KERNEL;
# Line 3656  int addNewKernel(struct grubConfig * con Line 3694  int addNewKernel(struct grubConfig * con
3694      if (needs & NEED_KERNEL) {      if (needs & NEED_KERNEL) {
3695   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
3696    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
3697        config->cfi)) ?        config->cfi))
3698    LT_MBMODULE : LT_KERNEL,     ? LT_MBMODULE
3699     : preferredLineType(LT_KERNEL, config->cfi),
3700    config->secondaryIndent,    config->secondaryIndent,
3701    newKernelPath + strlen(prefix));    newKernelPath + strlen(prefix));
3702   needs &= ~NEED_KERNEL;   needs &= ~NEED_KERNEL;
# Line 3673  int addNewKernel(struct grubConfig * con Line 3712  int addNewKernel(struct grubConfig * con
3712   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);   initrdVal = getInitrdVal(config, prefix, NULL, newKernelInitrd, extraInitrds, extraInitrdCount);
3713   newLine = addLine(new, config->cfi,   newLine = addLine(new, config->cfi,
3714    (new->multiboot && getKeywordByType(LT_MBMODULE,    (new->multiboot && getKeywordByType(LT_MBMODULE,
3715        config->cfi)) ?        config->cfi))
3716    LT_MBMODULE : LT_INITRD,     ? LT_MBMODULE
3717       : preferredLineType(LT_INITRD, config->cfi),
3718    config->secondaryIndent,    config->secondaryIndent,
3719    initrdVal);    initrdVal);
3720   free(initrdVal);   free(initrdVal);
# Line 3790  int main(int argc, const char ** argv) { Line 3830  int main(int argc, const char ** argv) {
3830      _("display the title of the default kernel") },      _("display the title of the default kernel") },
3831   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,   { "elilo", 0, POPT_ARG_NONE, &configureELilo, 0,
3832      _("configure elilo bootloader") },      _("configure elilo bootloader") },
3833     { "efi", 0, POPT_ARG_NONE, &isEfi, 0,
3834        _("force grub2 stanzas to use efi") },
3835   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,   { "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0,
3836      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
3837   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
# Line 4102  int main(int argc, const char ** argv) { Line 4144  int main(int argc, const char ** argv) {
4144   if (!entry) return 0;   if (!entry) return 0;
4145   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;   if (!suitableImage(entry, bootPrefix, 0, flags)) return 0;
4146    
4147   line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines);   line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines);
4148   if (!line) return 0;   if (!line) return 0;
4149    
4150          rootspec = getRootSpecifier(line->elements[1].item);          rootspec = getRootSpecifier(line->elements[1].item);

Legend:
Removed from v.1934  
changed lines
  Added in v.1940