36 |
#include <signal.h> |
#include <signal.h> |
37 |
#include <blkid/blkid.h> |
#include <blkid/blkid.h> |
38 |
|
|
39 |
|
#include "log.h" |
40 |
|
|
41 |
#ifndef DEBUG |
#ifndef DEBUG |
42 |
#define DEBUG 0 |
#define DEBUG 0 |
43 |
#endif |
#endif |
60 |
|
|
61 |
int isEfi = 0; |
int isEfi = 0; |
62 |
|
|
63 |
|
char *saved_command_line = NULL; |
64 |
|
|
65 |
/* comments get lumped in with indention */ |
/* comments get lumped in with indention */ |
66 |
struct lineElement { |
struct lineElement { |
67 |
char * item; |
char * item; |
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; |
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; |
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[] = { |
236 |
}; |
}; |
237 |
static int i = -1; |
static int i = -1; |
238 |
static const char *grub_cfg = "/boot/grub/grub.cfg"; |
static const char *grub_cfg = "/boot/grub/grub.cfg"; |
239 |
|
int rc = -1; |
240 |
|
|
241 |
if (i == -1) { |
if (i == -1) { |
242 |
for (i = 0; configFiles[i] != NULL; i++) { |
for (i = 0; configFiles[i] != NULL; i++) { |
243 |
dbgPrintf("Checking \"%s\": ", configFiles[i]); |
dbgPrintf("Checking \"%s\": ", configFiles[i]); |
244 |
if (!access(configFiles[i], R_OK)) { |
if ((rc = access(configFiles[i], R_OK))) { |
245 |
|
if (errno == EACCES) { |
246 |
|
printf("Unable to access bootloader configuration file " |
247 |
|
"\"%s\": %m\n", configFiles[i]); |
248 |
|
exit(1); |
249 |
|
} |
250 |
|
continue; |
251 |
|
} else { |
252 |
dbgPrintf("found\n"); |
dbgPrintf("found\n"); |
253 |
return configFiles[i]; |
return configFiles[i]; |
254 |
} |
} |
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 |
|
|
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, |
573 |
struct configFileInfo extlinuxConfigType = { |
struct configFileInfo extlinuxConfigType = { |
574 |
.defaultConfig = "/boot/extlinux/extlinux.conf", |
.defaultConfig = "/boot/extlinux/extlinux.conf", |
575 |
.keywords = extlinuxKeywords, |
.keywords = extlinuxKeywords, |
576 |
|
.caseInsensitive = 1, |
577 |
.entryStart = LT_TITLE, |
.entryStart = LT_TITLE, |
578 |
.needsBootPrefix = 1, |
.needsBootPrefix = 1, |
579 |
.maxTitleLength = 255, |
.maxTitleLength = 255, |
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); |
712 |
static enum lineType_e getTypeByKeyword(char * keyword, |
static enum lineType_e getTypeByKeyword(char * keyword, |
713 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
714 |
for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) { |
for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) { |
715 |
if (!strcmp(keyword, kw->key)) |
if (cfi->caseInsensitive) { |
716 |
return kw->type; |
if (!strcasecmp(keyword, kw->key)) |
717 |
|
return kw->type; |
718 |
|
} else { |
719 |
|
if (!strcmp(keyword, kw->key)) |
720 |
|
return kw->type; |
721 |
|
} |
722 |
} |
} |
723 |
return LT_UNKNOWN; |
return LT_UNKNOWN; |
724 |
} |
} |
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 |
|
|
1037 |
int len; |
int len; |
1038 |
char * buf; |
char * buf; |
1039 |
|
|
1040 |
if (!strcmp(inName, "-")) { |
if (inName == NULL) { |
1041 |
|
printf("Could not find bootloader configuration\n"); |
1042 |
|
exit(1); |
1043 |
|
} else if (!strcmp(inName, "-")) { |
1044 |
in = 0; |
in = 0; |
1045 |
} else { |
} else { |
1046 |
if ((in = open(inName, O_RDONLY)) < 0) { |
if ((in = open(inName, O_RDONLY)) < 0) { |
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; |
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 == '\'' || |
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 |
} |
} |
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, |
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) { |
1652 |
return NULL; |
return NULL; |
1653 |
} |
} |
1654 |
|
|
1655 |
void printEntry(struct singleEntry * entry) { |
void printEntry(struct singleEntry * entry, FILE *f) { |
1656 |
int i; |
int i; |
1657 |
struct singleLine * line; |
struct singleLine * line; |
1658 |
|
|
1659 |
for (line = entry->lines; line; line = line->next) { |
for (line = entry->lines; line; line = line->next) { |
1660 |
fprintf(stderr, "DBG: %s", line->indent); |
log_message(f, "DBG: %s", line->indent); |
1661 |
for (i = 0; i < line->numElements; i++) { |
for (i = 0; i < line->numElements; i++) { |
1662 |
/* Need to handle this, because we strip the quotes from |
/* Need to handle this, because we strip the quotes from |
1663 |
* menuentry when read it. */ |
* menuentry when read it. */ |
1664 |
if (line->type == LT_MENUENTRY && i == 1) { |
if (line->type == LT_MENUENTRY && i == 1) { |
1665 |
if(!isquote(*line->elements[i].item)) |
if(!isquote(*line->elements[i].item)) |
1666 |
fprintf(stderr, "\'%s\'", line->elements[i].item); |
log_message(f, "\'%s\'", line->elements[i].item); |
1667 |
else |
else |
1668 |
fprintf(stderr, "%s", line->elements[i].item); |
log_message(f, "%s", line->elements[i].item); |
1669 |
fprintf(stderr, "%s", line->elements[i].indent); |
log_message(f, "%s", line->elements[i].indent); |
1670 |
|
|
1671 |
continue; |
continue; |
1672 |
} |
} |
1673 |
|
|
1674 |
fprintf(stderr, "%s%s", |
log_message(f, "%s%s", |
1675 |
line->elements[i].item, line->elements[i].indent); |
line->elements[i].item, line->elements[i].indent); |
1676 |
} |
} |
1677 |
fprintf(stderr, "\n"); |
log_message(f, "\n"); |
1678 |
} |
} |
1679 |
} |
} |
1680 |
|
|
1681 |
void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...) |
void notSuitablePrintf(struct singleEntry * entry, int okay, const char *fmt, ...) |
1682 |
{ |
{ |
1683 |
va_list argp; |
static int once; |
1684 |
|
va_list argp, argq; |
1685 |
|
|
1686 |
if (!debug) |
va_start(argp, fmt); |
1687 |
|
|
1688 |
|
va_copy(argq, argp); |
1689 |
|
if (!once) { |
1690 |
|
log_time(NULL); |
1691 |
|
log_message(NULL, "command line: %s\n", saved_command_line); |
1692 |
|
} |
1693 |
|
log_message(NULL, "DBG: Image entry %s: ", okay ? "succeeded" : "failed"); |
1694 |
|
log_vmessage(NULL, fmt, argq); |
1695 |
|
|
1696 |
|
printEntry(entry, NULL); |
1697 |
|
va_end(argq); |
1698 |
|
|
1699 |
|
if (!debug) { |
1700 |
|
once = 1; |
1701 |
|
va_end(argp); |
1702 |
return; |
return; |
1703 |
|
} |
1704 |
|
|
1705 |
va_start(argp, fmt); |
if (okay) { |
1706 |
|
va_end(argp); |
1707 |
|
return; |
1708 |
|
} |
1709 |
|
|
1710 |
|
if (!once) |
1711 |
|
log_message(stderr, "DBG: command line: %s\n", saved_command_line); |
1712 |
|
once = 1; |
1713 |
fprintf(stderr, "DBG: Image entry failed: "); |
fprintf(stderr, "DBG: Image entry failed: "); |
1714 |
vfprintf(stderr, fmt, argp); |
vfprintf(stderr, fmt, argp); |
1715 |
printEntry(entry); |
printEntry(entry, stderr); |
1716 |
va_end(argp); |
va_end(argp); |
1717 |
} |
} |
1718 |
|
|
1739 |
char * rootdev; |
char * rootdev; |
1740 |
|
|
1741 |
if (skipRemoved && entry->skip) { |
if (skipRemoved && entry->skip) { |
1742 |
notSuitablePrintf(entry, "marked to skip\n"); |
notSuitablePrintf(entry, 0, "marked to skip\n"); |
1743 |
return 0; |
return 0; |
1744 |
} |
} |
1745 |
|
|
1746 |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER|LT_KERNEL_EFI, entry->lines); |
1747 |
if (!line) { |
if (!line) { |
1748 |
notSuitablePrintf(entry, "no line found\n"); |
notSuitablePrintf(entry, 0, "no line found\n"); |
1749 |
return 0; |
return 0; |
1750 |
} |
} |
1751 |
if (line->numElements < 2) { |
if (line->numElements < 2) { |
1752 |
notSuitablePrintf(entry, "line has only %d elements\n", |
notSuitablePrintf(entry, 0, "line has only %d elements\n", |
1753 |
line->numElements); |
line->numElements); |
1754 |
return 0; |
return 0; |
1755 |
} |
} |
1756 |
|
|
1757 |
if (flags & GRUBBY_BADIMAGE_OKAY) return 1; |
if (flags & GRUBBY_BADIMAGE_OKAY) { |
1758 |
|
notSuitablePrintf(entry, 1, "\n"); |
1759 |
|
return 1; |
1760 |
|
} |
1761 |
|
|
1762 |
fullName = alloca(strlen(bootPrefix) + |
fullName = alloca(strlen(bootPrefix) + |
1763 |
strlen(line->elements[1].item) + 1); |
strlen(line->elements[1].item) + 1); |
1768 |
sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/", |
sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/", |
1769 |
line->elements[1].item + rootspec_offset); |
line->elements[1].item + rootspec_offset); |
1770 |
if (access(fullName, R_OK)) { |
if (access(fullName, R_OK)) { |
1771 |
notSuitablePrintf(entry, "access to %s failed\n", fullName); |
notSuitablePrintf(entry, 0, "access to %s failed\n", fullName); |
1772 |
return 0; |
return 0; |
1773 |
} |
} |
1774 |
for (i = 2; i < line->numElements; i++) |
for (i = 2; i < line->numElements; i++) |
1789 |
|
|
1790 |
/* failed to find one */ |
/* failed to find one */ |
1791 |
if (!line) { |
if (!line) { |
1792 |
notSuitablePrintf(entry, "no line found\n"); |
notSuitablePrintf(entry, 0, "no line found\n"); |
1793 |
return 0; |
return 0; |
1794 |
} |
} |
1795 |
|
|
1798 |
if (i < line->numElements) |
if (i < line->numElements) |
1799 |
dev = line->elements[i].item + 5; |
dev = line->elements[i].item + 5; |
1800 |
else { |
else { |
1801 |
notSuitablePrintf(entry, "no root= entry found\n"); |
notSuitablePrintf(entry, 0, "no root= entry found\n"); |
1802 |
/* it failed too... can't find root= */ |
/* it failed too... can't find root= */ |
1803 |
return 0; |
return 0; |
1804 |
} |
} |
1807 |
|
|
1808 |
dev = getpathbyspec(dev); |
dev = getpathbyspec(dev); |
1809 |
if (!getpathbyspec(dev)) { |
if (!getpathbyspec(dev)) { |
1810 |
notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev); |
notSuitablePrintf(entry, 0, "can't find blkid entry for %s\n", dev); |
1811 |
return 0; |
return 0; |
1812 |
} else |
} else |
1813 |
dev = getpathbyspec(dev); |
dev = getpathbyspec(dev); |
1814 |
|
|
1815 |
rootdev = findDiskForRoot(); |
rootdev = findDiskForRoot(); |
1816 |
if (!rootdev) { |
if (!rootdev) { |
1817 |
notSuitablePrintf(entry, "can't find root device\n"); |
notSuitablePrintf(entry, 0, "can't find root device\n"); |
1818 |
return 0; |
return 0; |
1819 |
} |
} |
1820 |
|
|
1821 |
if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) { |
if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) { |
1822 |
notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n", |
notSuitablePrintf(entry, 0, "uuid missing: rootdev %s, dev %s\n", |
1823 |
getuuidbydev(rootdev), getuuidbydev(dev)); |
getuuidbydev(rootdev), getuuidbydev(dev)); |
1824 |
free(rootdev); |
free(rootdev); |
1825 |
return 0; |
return 0; |
1826 |
} |
} |
1827 |
|
|
1828 |
if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) { |
if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) { |
1829 |
notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n", |
notSuitablePrintf(entry, 0, "uuid mismatch: rootdev %s, dev %s\n", |
1830 |
getuuidbydev(rootdev), getuuidbydev(dev)); |
getuuidbydev(rootdev), getuuidbydev(dev)); |
1831 |
free(rootdev); |
free(rootdev); |
1832 |
return 0; |
return 0; |
1833 |
} |
} |
1834 |
|
|
1835 |
free(rootdev); |
free(rootdev); |
1836 |
|
notSuitablePrintf(entry, 1, "\n"); |
1837 |
|
|
1838 |
return 1; |
return 1; |
1839 |
} |
} |
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 |
|
|
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; |
3941 |
memset(array, '\0', sizeof (array)); |
memset(array, '\0', sizeof (array)); |
3942 |
size = backtrace(array, 40); |
size = backtrace(array, 40); |
3943 |
|
|
3944 |
fprintf(stderr, "grubby recieved SIGSEGV! Backtrace (%ld):\n", |
fprintf(stderr, "grubby received SIGSEGV! Backtrace (%ld):\n", |
3945 |
(unsigned long)size); |
(unsigned long)size); |
3946 |
backtrace_symbols_fd(array, size, STDERR_FILENO); |
backtrace_symbols_fd(array, size, STDERR_FILENO); |
3947 |
exit(1); |
exit(1); |
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; |
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, |
4043 |
{ "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0, |
{ "initrd", 0, POPT_ARG_STRING, &newKernelInitrd, 0, |
4044 |
_("initrd image for the new kernel"), _("initrd-path") }, |
_("initrd image for the new kernel"), _("initrd-path") }, |
4045 |
{ "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i', |
{ "extra-initrd", 'i', POPT_ARG_STRING, NULL, 'i', |
4046 |
_("auxilliary initrd image for things other than the new kernel"), _("initrd-path") }, |
_("auxiliary initrd image for things other than the new kernel"), _("initrd-path") }, |
4047 |
{ "lilo", 0, POPT_ARG_NONE, &configureLilo, 0, |
{ "lilo", 0, POPT_ARG_NONE, &configureLilo, 0, |
4048 |
_("configure lilo bootloader") }, |
_("configure lilo bootloader") }, |
4049 |
{ "make-default", 0, 0, &makeDefault, 0, |
{ "make-default", 0, 0, &makeDefault, 0, |
4087 |
|
|
4088 |
signal(SIGSEGV, traceback); |
signal(SIGSEGV, traceback); |
4089 |
|
|
4090 |
|
int i = 0; |
4091 |
|
for (int j = 1; j < argc; j++) |
4092 |
|
i += strlen(argv[j]) + 1; |
4093 |
|
saved_command_line = malloc(i); |
4094 |
|
if (!saved_command_line) { |
4095 |
|
fprintf(stderr, "grubby: %m\n"); |
4096 |
|
exit(1); |
4097 |
|
} |
4098 |
|
saved_command_line[0] = '\0'; |
4099 |
|
for (int j = 1; j < argc; j++) { |
4100 |
|
strcat(saved_command_line, argv[j]); |
4101 |
|
strncat(saved_command_line, j == argc -1 ? "" : " ", 1); |
4102 |
|
} |
4103 |
|
|
4104 |
optCon = poptGetContext("grubby", argc, argv, options, 0); |
optCon = poptGetContext("grubby", argc, argv, options, 0); |
4105 |
poptReadDefaultConfig(optCon, 1); |
poptReadDefaultConfig(optCon, 1); |
4106 |
|
|
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) { |
4346 |
return 0; |
return 0; |
4347 |
} |
} |
4348 |
|
|
4349 |
|
if (grubConfig == NULL) { |
4350 |
|
printf("Could not find bootloader configuration file.\n"); |
4351 |
|
exit(1); |
4352 |
|
} |
4353 |
|
|
4354 |
config = readConfig(grubConfig, cfi); |
config = readConfig(grubConfig, cfi); |
4355 |
if (!config) return 1; |
if (!config) return 1; |
4356 |
|
|
4360 |
char * rootspec; |
char * rootspec; |
4361 |
|
|
4362 |
if (config->defaultImage == -1) return 0; |
if (config->defaultImage == -1) return 0; |
4363 |
|
if (config->defaultImage == DEFAULT_SAVED_GRUB2 && |
4364 |
|
cfi->defaultIsSaved) |
4365 |
|
config->defaultImage = 0; |
4366 |
entry = findEntryByIndex(config, config->defaultImage); |
entry = findEntryByIndex(config, config->defaultImage); |
4367 |
if (!entry) return 0; |
if (!entry) return 0; |
4368 |
if (!suitableImage(entry, bootPrefix, 0, flags)) return 0; |
if (!suitableImage(entry, bootPrefix, 0, flags)) return 0; |
4381 |
struct singleEntry * entry; |
struct singleEntry * entry; |
4382 |
|
|
4383 |
if (config->defaultImage == -1) return 0; |
if (config->defaultImage == -1) return 0; |
4384 |
|
if (config->defaultImage == DEFAULT_SAVED_GRUB2 && |
4385 |
|
cfi->defaultIsSaved) |
4386 |
|
config->defaultImage = 0; |
4387 |
entry = findEntryByIndex(config, config->defaultImage); |
entry = findEntryByIndex(config, config->defaultImage); |
4388 |
if (!entry) return 0; |
if (!entry) return 0; |
4389 |
|
|
4406 |
|
|
4407 |
} else if (displayDefaultIndex) { |
} else if (displayDefaultIndex) { |
4408 |
if (config->defaultImage == -1) return 0; |
if (config->defaultImage == -1) return 0; |
4409 |
|
if (config->defaultImage == DEFAULT_SAVED_GRUB2 && |
4410 |
|
cfi->defaultIsSaved) |
4411 |
|
config->defaultImage = 0; |
4412 |
printf("%i\n", config->defaultImage); |
printf("%i\n", config->defaultImage); |
4413 |
|
return 0; |
4414 |
|
|
4415 |
} else if (kernelInfo) |
} else if (kernelInfo) |
4416 |
return displayInfo(config, kernelInfo, bootPrefix); |
return displayInfo(config, kernelInfo, bootPrefix); |