46 |
#define dbgPrintf(format, args...) |
#define dbgPrintf(format, args...) |
47 |
#endif |
#endif |
48 |
|
|
49 |
|
int debug = 0; /* Currently just for template debugging */ |
50 |
|
|
51 |
#define _(A) (A) |
#define _(A) (A) |
52 |
|
|
53 |
#define MAX_EXTRA_INITRDS 16 /* code segment checked by --bootloader-probe */ |
#define MAX_EXTRA_INITRDS 16 /* code segment checked by --bootloader-probe */ |
54 |
#define CODE_SEG_SIZE 128 /* code segment checked by --bootloader-probe */ |
#define CODE_SEG_SIZE 128 /* code segment checked by --bootloader-probe */ |
55 |
|
|
56 |
|
#define NOOP_OPCODE 0x90 |
57 |
|
#define JMP_SHORT_OPCODE 0xeb |
58 |
|
|
59 |
/* comments get lumped in with indention */ |
/* comments get lumped in with indention */ |
60 |
struct lineElement { |
struct lineElement { |
61 |
char * item; |
char * item; |
114 |
|
|
115 |
#define MAIN_DEFAULT (1 << 0) |
#define MAIN_DEFAULT (1 << 0) |
116 |
#define DEFAULT_SAVED -2 |
#define DEFAULT_SAVED -2 |
117 |
|
#define DEFAULT_SAVED_GRUB2 -3 |
118 |
|
|
119 |
struct keywordTypes { |
struct keywordTypes { |
120 |
char * key; |
char * key; |
162 |
{ NULL, 0, 0 }, |
{ NULL, 0, 0 }, |
163 |
}; |
}; |
164 |
|
|
165 |
|
const char *grubFindConfig(struct configFileInfo *cfi) { |
166 |
|
static const char *configFiles[] = { |
167 |
|
"/etc/grub.conf", |
168 |
|
"/boot/grub/grub.conf", |
169 |
|
"/boot/grub/menu.lst", |
170 |
|
NULL |
171 |
|
}; |
172 |
|
static int i = -1; |
173 |
|
|
174 |
|
if (i == -1) { |
175 |
|
for (i = 0; configFiles[i] != NULL; i++) { |
176 |
|
dbgPrintf("Checking \"%s\": ", configFiles[i]); |
177 |
|
if (!access(configFiles[i], R_OK)) { |
178 |
|
dbgPrintf("found\n"); |
179 |
|
return configFiles[i]; |
180 |
|
} |
181 |
|
dbgPrintf("not found\n"); |
182 |
|
} |
183 |
|
} |
184 |
|
return configFiles[i]; |
185 |
|
} |
186 |
|
|
187 |
struct configFileInfo grubConfigType = { |
struct configFileInfo grubConfigType = { |
188 |
.defaultConfig = "/boot/grub/grub.conf", |
.findConfig = grubFindConfig, |
189 |
.keywords = grubKeywords, |
.keywords = grubKeywords, |
190 |
.defaultIsIndex = 1, |
.defaultIsIndex = 1, |
191 |
.defaultSupportSaved = 1, |
.defaultSupportSaved = 1, |
218 |
NULL |
NULL |
219 |
}; |
}; |
220 |
static int i = -1; |
static int i = -1; |
221 |
|
static const char *grub_cfg = "/boot/grub/grub.cfg"; |
222 |
|
|
223 |
if (i == -1) { |
if (i == -1) { |
224 |
for (i = 0; configFiles[i] != NULL; i++) { |
for (i = 0; configFiles[i] != NULL; i++) { |
227 |
dbgPrintf("found\n"); |
dbgPrintf("found\n"); |
228 |
return configFiles[i]; |
return configFiles[i]; |
229 |
} |
} |
|
dbgPrintf("not found\n"); |
|
230 |
} |
} |
231 |
} |
} |
232 |
|
|
233 |
|
/* Ubuntu renames grub2 to grub, so check for the grub.d directory |
234 |
|
* that isn't in grub1, and if it exists, return the config file path |
235 |
|
* that they use. */ |
236 |
|
if (configFiles[i] == NULL && !access("/etc/grub.d/", R_OK)) { |
237 |
|
dbgPrintf("found\n"); |
238 |
|
return grub_cfg; |
239 |
|
} |
240 |
|
|
241 |
|
dbgPrintf("not found\n"); |
242 |
return configFiles[i]; |
return configFiles[i]; |
243 |
} |
} |
244 |
|
|
245 |
|
int sizeOfSingleLine(struct singleLine * line) { |
246 |
|
int i; |
247 |
|
int count = 0; |
248 |
|
|
249 |
|
for (i = 0; i < line->numElements; i++) { |
250 |
|
int indentSize = 0; |
251 |
|
|
252 |
|
count = count + strlen(line->elements[i].item); |
253 |
|
|
254 |
|
indentSize = strlen(line->elements[i].indent); |
255 |
|
if (indentSize > 0) |
256 |
|
count = count + indentSize; |
257 |
|
else |
258 |
|
/* be extra safe and add room for whitespaces */ |
259 |
|
count = count + 1; |
260 |
|
} |
261 |
|
|
262 |
|
/* room for trailing terminator */ |
263 |
|
count = count + 1; |
264 |
|
|
265 |
|
return count; |
266 |
|
} |
267 |
|
|
268 |
|
static int isquote(char q) |
269 |
|
{ |
270 |
|
if (q == '\'' || q == '\"') |
271 |
|
return 1; |
272 |
|
return 0; |
273 |
|
} |
274 |
|
|
275 |
|
char *grub2ExtractTitle(struct singleLine * line) { |
276 |
|
char * current; |
277 |
|
char * current_indent; |
278 |
|
int current_len; |
279 |
|
int current_indent_len; |
280 |
|
int i; |
281 |
|
|
282 |
|
/* bail out if line does not start with menuentry */ |
283 |
|
if (strcmp(line->elements[0].item, "menuentry")) |
284 |
|
return NULL; |
285 |
|
|
286 |
|
i = 1; |
287 |
|
current = line->elements[i].item; |
288 |
|
current_len = strlen(current); |
289 |
|
|
290 |
|
/* if second word is quoted, strip the quotes and return single word */ |
291 |
|
if (isquote(*current) && isquote(current[current_len - 1])) { |
292 |
|
char *tmp; |
293 |
|
|
294 |
|
tmp = strdup(current); |
295 |
|
*(tmp + current_len - 1) = '\0'; |
296 |
|
return ++tmp; |
297 |
|
} |
298 |
|
|
299 |
|
/* if no quotes, return second word verbatim */ |
300 |
|
if (!isquote(*current)) |
301 |
|
return current; |
302 |
|
|
303 |
|
/* second element start with a quote, so we have to find the element |
304 |
|
* whose last character is also quote (assuming it's the closing one) */ |
305 |
|
int resultMaxSize; |
306 |
|
char * result; |
307 |
|
|
308 |
|
resultMaxSize = sizeOfSingleLine(line); |
309 |
|
result = malloc(resultMaxSize); |
310 |
|
snprintf(result, resultMaxSize, "%s", ++current); |
311 |
|
|
312 |
|
i++; |
313 |
|
for (; i < line->numElements; ++i) { |
314 |
|
current = line->elements[i].item; |
315 |
|
current_len = strlen(current); |
316 |
|
current_indent = line->elements[i].indent; |
317 |
|
current_indent_len = strlen(current_indent); |
318 |
|
|
319 |
|
strncat(result, current_indent, current_indent_len); |
320 |
|
if (!isquote(current[current_len-1])) { |
321 |
|
strncat(result, current, current_len); |
322 |
|
} else { |
323 |
|
strncat(result, current, current_len - 1); |
324 |
|
break; |
325 |
|
} |
326 |
|
} |
327 |
|
return result; |
328 |
|
} |
329 |
|
|
330 |
struct configFileInfo grub2ConfigType = { |
struct configFileInfo grub2ConfigType = { |
331 |
.findConfig = grub2FindConfig, |
.findConfig = grub2FindConfig, |
332 |
.keywords = grub2Keywords, |
.keywords = grub2Keywords, |
333 |
.defaultIsIndex = 1, |
.defaultIsIndex = 1, |
334 |
.defaultSupportSaved = 0, |
.defaultSupportSaved = 1, |
335 |
.defaultIsVariable = 1, |
.defaultIsVariable = 1, |
336 |
.entryStart = LT_MENUENTRY, |
.entryStart = LT_MENUENTRY, |
337 |
.entryEnd = LT_ENTRY_END, |
.entryEnd = LT_ENTRY_END, |
731 |
if (fprintf(out, "%s", line->indent) == -1) return -1; |
if (fprintf(out, "%s", line->indent) == -1) return -1; |
732 |
|
|
733 |
for (i = 0; i < line->numElements; i++) { |
for (i = 0; i < line->numElements; i++) { |
734 |
|
/* Need to handle this, because we strip the quotes from |
735 |
|
* menuentry when read it. */ |
736 |
|
if (line->type == LT_MENUENTRY && i == 1) { |
737 |
|
if(!isquote(*line->elements[i].item)) |
738 |
|
fprintf(out, "\'%s\'", line->elements[i].item); |
739 |
|
else |
740 |
|
fprintf(out, "%s", line->elements[i].item); |
741 |
|
fprintf(out, "%s", line->elements[i].indent); |
742 |
|
|
743 |
|
continue; |
744 |
|
} |
745 |
|
|
746 |
if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes) |
if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes) |
747 |
if (fputc('"', out) == EOF) return -1; |
if (fputc('"', out) == EOF) return -1; |
748 |
|
|
974 |
cfg->secondaryIndent = strdup(line->indent); |
cfg->secondaryIndent = strdup(line->indent); |
975 |
} |
} |
976 |
|
|
977 |
if (isEntryStart(line, cfi)) { |
if (isEntryStart(line, cfi) || (cfg->entries && !sawEntry)) { |
978 |
sawEntry = 1; |
sawEntry = 1; |
979 |
if (!entry) { |
if (!entry) { |
980 |
cfg->entries = malloc(sizeof(*entry)); |
cfg->entries = malloc(sizeof(*entry)); |
1064 |
line->elements[line->numElements - 1].indent; |
line->elements[line->numElements - 1].indent; |
1065 |
line->elements[1].item = buf; |
line->elements[1].item = buf; |
1066 |
line->numElements = 2; |
line->numElements = 2; |
1067 |
|
} else if (line->type == LT_MENUENTRY && line->numElements > 3) { |
1068 |
|
/* let --remove-kernel="TITLE=what" work */ |
1069 |
|
len = 0; |
1070 |
|
char *extras; |
1071 |
|
char *title; |
1072 |
|
|
1073 |
|
for (i = 1; i < line->numElements; i++) { |
1074 |
|
len += strlen(line->elements[i].item); |
1075 |
|
len += strlen(line->elements[i].indent); |
1076 |
|
} |
1077 |
|
buf = malloc(len + 1); |
1078 |
|
*buf = '\0'; |
1079 |
|
|
1080 |
|
/* allocate mem for extra flags. */ |
1081 |
|
extras = malloc(len + 1); |
1082 |
|
*extras = '\0'; |
1083 |
|
|
1084 |
|
/* get title. */ |
1085 |
|
for (i = 0; i < line->numElements; i++) { |
1086 |
|
if (!strcmp(line->elements[i].item, "menuentry")) |
1087 |
|
continue; |
1088 |
|
if (isquote(*line->elements[i].item)) |
1089 |
|
title = line->elements[i].item + 1; |
1090 |
|
else |
1091 |
|
title = line->elements[i].item; |
1092 |
|
|
1093 |
|
len = strlen(title); |
1094 |
|
if (isquote(title[len-1])) { |
1095 |
|
strncat(buf, title,len-1); |
1096 |
|
break; |
1097 |
|
} else { |
1098 |
|
strcat(buf, title); |
1099 |
|
strcat(buf, line->elements[i].indent); |
1100 |
|
} |
1101 |
|
} |
1102 |
|
|
1103 |
|
/* get extras */ |
1104 |
|
int count = 0; |
1105 |
|
for (i = 0; i < line->numElements; i++) { |
1106 |
|
if (count == 2) { |
1107 |
|
strcat(extras, line->elements[i].item); |
1108 |
|
strcat(extras, line->elements[i].indent); |
1109 |
|
} |
1110 |
|
|
1111 |
|
if (!strcmp(line->elements[i].item, "menuentry")) |
1112 |
|
continue; |
1113 |
|
|
1114 |
|
/* count ' or ", there should be two in menuentry line. */ |
1115 |
|
if (isquote(*line->elements[i].item)) |
1116 |
|
count++; |
1117 |
|
|
1118 |
|
len = strlen(line->elements[i].item); |
1119 |
|
|
1120 |
|
if (isquote(line->elements[i].item[len -1])) |
1121 |
|
count++; |
1122 |
|
|
1123 |
|
/* ok, we get the final ' or ", others are extras. */ |
1124 |
|
} |
1125 |
|
line->elements[1].indent = |
1126 |
|
line->elements[line->numElements - 2].indent; |
1127 |
|
line->elements[1].item = buf; |
1128 |
|
line->elements[2].indent = |
1129 |
|
line->elements[line->numElements - 2].indent; |
1130 |
|
line->elements[2].item = extras; |
1131 |
|
line->numElements = 3; |
1132 |
} else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) { |
} else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) { |
1133 |
/* Strip off any " which may be present; they'll be put back |
/* Strip off any " which may be present; they'll be put back |
1134 |
on write. This is one of the few (the only?) places that grubby |
on write. This is one of the few (the only?) places that grubby |
1137 |
if (line->numElements >= 2) { |
if (line->numElements >= 2) { |
1138 |
int last, len; |
int last, len; |
1139 |
|
|
1140 |
if (*line->elements[1].item == '"') |
if (isquote(*line->elements[1].item)) |
1141 |
memmove(line->elements[1].item, line->elements[1].item + 1, |
memmove(line->elements[1].item, line->elements[1].item + 1, |
1142 |
strlen(line->elements[1].item + 1) + 1); |
strlen(line->elements[1].item + 1) + 1); |
1143 |
|
|
1144 |
last = line->numElements - 1; |
last = line->numElements - 1; |
1145 |
len = strlen(line->elements[last].item) - 1; |
len = strlen(line->elements[last].item) - 1; |
1146 |
if (line->elements[last].item[len] == '"') |
if (isquote(line->elements[last].item[len])) |
1147 |
line->elements[last].item[len] = '\0'; |
line->elements[last].item[len] = '\0'; |
1148 |
} |
} |
1149 |
} |
} |
1202 |
|
|
1203 |
dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset"); |
dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset"); |
1204 |
if (defaultLine) { |
if (defaultLine) { |
1205 |
if (cfi->defaultIsVariable) { |
if (defaultLine->numElements > 2 && |
1206 |
|
cfi->defaultSupportSaved && |
1207 |
|
!strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) { |
1208 |
|
cfg->defaultImage = DEFAULT_SAVED_GRUB2; |
1209 |
|
} else if (cfi->defaultIsVariable) { |
1210 |
char *value = defaultLine->elements[2].item; |
char *value = defaultLine->elements[2].item; |
1211 |
while (*value && (*value == '"' || *value == '\'' || |
while (*value && (*value == '"' || *value == '\'' || |
1212 |
*value == ' ' || *value == '\t')) |
*value == ' ' || *value == '\t')) |
1263 |
|
|
1264 |
if (cfg->defaultImage == DEFAULT_SAVED) |
if (cfg->defaultImage == DEFAULT_SAVED) |
1265 |
fprintf(out, "%sdefault%ssaved\n", indent, separator); |
fprintf(out, "%sdefault%ssaved\n", indent, separator); |
1266 |
|
else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2) |
1267 |
|
fprintf(out, "%sset default=\"${saved_entry}\"\n", indent); |
1268 |
else if (cfg->defaultImage > -1) { |
else if (cfg->defaultImage > -1) { |
1269 |
if (cfg->cfi->defaultIsIndex) { |
if (cfg->cfi->defaultIsIndex) { |
1270 |
if (cfg->cfi->defaultIsVariable) { |
if (cfg->cfi->defaultIsVariable) { |
1508 |
return NULL; |
return NULL; |
1509 |
} |
} |
1510 |
|
|
1511 |
|
void printEntry(struct singleEntry * entry) { |
1512 |
|
int i; |
1513 |
|
struct singleLine * line; |
1514 |
|
|
1515 |
|
for (line = entry->lines; line; line = line->next) { |
1516 |
|
fprintf(stderr, "DBG: %s", line->indent); |
1517 |
|
for (i = 0; i < line->numElements; i++) { |
1518 |
|
/* Need to handle this, because we strip the quotes from |
1519 |
|
* menuentry when read it. */ |
1520 |
|
if (line->type == LT_MENUENTRY && i == 1) { |
1521 |
|
if(!isquote(*line->elements[i].item)) |
1522 |
|
fprintf(stderr, "\'%s\'", line->elements[i].item); |
1523 |
|
else |
1524 |
|
fprintf(stderr, "%s", line->elements[i].item); |
1525 |
|
fprintf(stderr, "%s", line->elements[i].indent); |
1526 |
|
|
1527 |
|
continue; |
1528 |
|
} |
1529 |
|
|
1530 |
|
fprintf(stderr, "%s%s", |
1531 |
|
line->elements[i].item, line->elements[i].indent); |
1532 |
|
} |
1533 |
|
fprintf(stderr, "\n"); |
1534 |
|
} |
1535 |
|
} |
1536 |
|
|
1537 |
|
void notSuitablePrintf(struct singleEntry * entry, const char *fmt, ...) |
1538 |
|
{ |
1539 |
|
va_list argp; |
1540 |
|
|
1541 |
|
if (!debug) |
1542 |
|
return; |
1543 |
|
|
1544 |
|
va_start(argp, fmt); |
1545 |
|
fprintf(stderr, "DBG: Image entry failed: "); |
1546 |
|
vfprintf(stderr, fmt, argp); |
1547 |
|
printEntry(entry); |
1548 |
|
va_end(argp); |
1549 |
|
} |
1550 |
|
|
1551 |
|
#define beginswith(s, c) ((s) && (s)[0] == (c)) |
1552 |
|
|
1553 |
|
static int endswith(const char *s, char c) |
1554 |
|
{ |
1555 |
|
int slen; |
1556 |
|
|
1557 |
|
if (!s || !s[0]) |
1558 |
|
return 0; |
1559 |
|
slen = strlen(s) - 1; |
1560 |
|
|
1561 |
|
return s[slen] == c; |
1562 |
|
} |
1563 |
|
|
1564 |
int suitableImage(struct singleEntry * entry, const char * bootPrefix, |
int suitableImage(struct singleEntry * entry, const char * bootPrefix, |
1565 |
int skipRemoved, int flags) { |
int skipRemoved, int flags) { |
1566 |
struct singleLine * line; |
struct singleLine * line; |
1570 |
char * rootspec; |
char * rootspec; |
1571 |
char * rootdev; |
char * rootdev; |
1572 |
|
|
1573 |
if (skipRemoved && entry->skip) return 0; |
if (skipRemoved && entry->skip) { |
1574 |
|
notSuitablePrintf(entry, "marked to skip\n"); |
1575 |
|
return 0; |
1576 |
|
} |
1577 |
|
|
1578 |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
line = getLineByType(LT_KERNEL|LT_HYPER, entry->lines); |
1579 |
if (!line || line->numElements < 2) return 0; |
if (!line) { |
1580 |
|
notSuitablePrintf(entry, "no line found\n"); |
1581 |
|
return 0; |
1582 |
|
} |
1583 |
|
if (line->numElements < 2) { |
1584 |
|
notSuitablePrintf(entry, "line has only %d elements\n", |
1585 |
|
line->numElements); |
1586 |
|
return 0; |
1587 |
|
} |
1588 |
|
|
1589 |
if (flags & GRUBBY_BADIMAGE_OKAY) return 1; |
if (flags & GRUBBY_BADIMAGE_OKAY) return 1; |
1590 |
|
|
1591 |
fullName = alloca(strlen(bootPrefix) + |
fullName = alloca(strlen(bootPrefix) + |
1592 |
strlen(line->elements[1].item) + 1); |
strlen(line->elements[1].item) + 1); |
1593 |
rootspec = getRootSpecifier(line->elements[1].item); |
rootspec = getRootSpecifier(line->elements[1].item); |
1594 |
sprintf(fullName, "%s%s", bootPrefix, |
int rootspec_offset = rootspec ? strlen(rootspec) : 0; |
1595 |
line->elements[1].item + (rootspec ? strlen(rootspec) : 0)); |
int hasslash = endswith(bootPrefix, '/') || |
1596 |
if (access(fullName, R_OK)) return 0; |
beginswith(line->elements[1].item + rootspec_offset, '/'); |
1597 |
|
sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/", |
1598 |
|
line->elements[1].item + rootspec_offset); |
1599 |
|
if (access(fullName, R_OK)) { |
1600 |
|
notSuitablePrintf(entry, "access to %s failed\n", fullName); |
1601 |
|
return 0; |
1602 |
|
} |
1603 |
for (i = 2; i < line->numElements; i++) |
for (i = 2; i < line->numElements; i++) |
1604 |
if (!strncasecmp(line->elements[i].item, "root=", 5)) break; |
if (!strncasecmp(line->elements[i].item, "root=", 5)) break; |
1605 |
if (i < line->numElements) { |
if (i < line->numElements) { |
1617 |
line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines); |
line = getLineByType(LT_KERNELARGS|LT_MBMODULE, entry->lines); |
1618 |
|
|
1619 |
/* failed to find one */ |
/* failed to find one */ |
1620 |
if (!line) return 0; |
if (!line) { |
1621 |
|
notSuitablePrintf(entry, "no line found\n"); |
1622 |
|
return 0; |
1623 |
|
} |
1624 |
|
|
1625 |
for (i = 1; i < line->numElements; i++) |
for (i = 1; i < line->numElements; i++) |
1626 |
if (!strncasecmp(line->elements[i].item, "root=", 5)) break; |
if (!strncasecmp(line->elements[i].item, "root=", 5)) break; |
1627 |
if (i < line->numElements) |
if (i < line->numElements) |
1628 |
dev = line->elements[i].item + 5; |
dev = line->elements[i].item + 5; |
1629 |
else { |
else { |
1630 |
|
notSuitablePrintf(entry, "no root= entry found\n"); |
1631 |
/* it failed too... can't find root= */ |
/* it failed too... can't find root= */ |
1632 |
return 0; |
return 0; |
1633 |
} |
} |
1635 |
} |
} |
1636 |
|
|
1637 |
dev = getpathbyspec(dev); |
dev = getpathbyspec(dev); |
1638 |
if (!dev) |
if (!getpathbyspec(dev)) { |
1639 |
|
notSuitablePrintf(entry, "can't find blkid entry for %s\n", dev); |
1640 |
return 0; |
return 0; |
1641 |
|
} else |
1642 |
|
dev = getpathbyspec(dev); |
1643 |
|
|
1644 |
rootdev = findDiskForRoot(); |
rootdev = findDiskForRoot(); |
1645 |
if (!rootdev) |
if (!rootdev) { |
1646 |
|
notSuitablePrintf(entry, "can't find root device\n"); |
1647 |
return 0; |
return 0; |
1648 |
|
} |
1649 |
|
|
1650 |
if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) { |
if (!getuuidbydev(rootdev) || !getuuidbydev(dev)) { |
1651 |
|
notSuitablePrintf(entry, "uuid missing: rootdev %s, dev %s\n", |
1652 |
|
getuuidbydev(rootdev), getuuidbydev(dev)); |
1653 |
free(rootdev); |
free(rootdev); |
1654 |
return 0; |
return 0; |
1655 |
} |
} |
1656 |
|
|
1657 |
if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) { |
if (strcmp(getuuidbydev(rootdev), getuuidbydev(dev))) { |
1658 |
|
notSuitablePrintf(entry, "uuid mismatch: rootdev %s, dev %s\n", |
1659 |
|
getuuidbydev(rootdev), getuuidbydev(dev)); |
1660 |
free(rootdev); |
free(rootdev); |
1661 |
return 0; |
return 0; |
1662 |
} |
} |
1743 |
|
|
1744 |
if (!strncmp(kernel, "TITLE=", 6)) { |
if (!strncmp(kernel, "TITLE=", 6)) { |
1745 |
prefix = ""; |
prefix = ""; |
1746 |
checkType = LT_TITLE; |
checkType = LT_TITLE|LT_MENUENTRY; |
1747 |
kernel += 6; |
kernel += 6; |
1748 |
} |
} |
1749 |
|
|
1759 |
checkType, line); |
checkType, line); |
1760 |
if (!line) break; /* not found in this entry */ |
if (!line) break; /* not found in this entry */ |
1761 |
|
|
1762 |
if (line && line->numElements >= 2) { |
if (line && line->type != LT_MENUENTRY && |
1763 |
|
line->numElements >= 2) { |
1764 |
rootspec = getRootSpecifier(line->elements[1].item); |
rootspec = getRootSpecifier(line->elements[1].item); |
1765 |
if (!strcmp(line->elements[1].item + |
if (!strcmp(line->elements[1].item + |
1766 |
((rootspec != NULL) ? strlen(rootspec) : 0), |
((rootspec != NULL) ? strlen(rootspec) : 0), |
1767 |
kernel + strlen(prefix))) |
kernel + strlen(prefix))) |
1768 |
break; |
break; |
1769 |
} |
} |
1770 |
|
if(line->type == LT_MENUENTRY && |
1771 |
|
!strcmp(line->elements[1].item, kernel)) |
1772 |
|
break; |
1773 |
} |
} |
1774 |
|
|
1775 |
/* make sure this entry has a kernel identifier; this skips |
/* make sure this entry has a kernel identifier; this skips |
1861 |
const char * prefix) { |
const char * prefix) { |
1862 |
struct singleEntry * entry; |
struct singleEntry * entry; |
1863 |
|
|
1864 |
if (!image) return; |
if (!image) |
1865 |
|
return; |
1866 |
|
|
1867 |
|
/* check and see if we're removing the default image */ |
1868 |
|
if (isdigit(*image)) { |
1869 |
|
entry = findEntryByPath(cfg, image, prefix, NULL); |
1870 |
|
if(entry) |
1871 |
|
entry->skip = 1; |
1872 |
|
return; |
1873 |
|
} |
1874 |
|
|
1875 |
while ((entry = findEntryByPath(cfg, image, prefix, NULL))) |
while ((entry = findEntryByPath(cfg, image, prefix, NULL))) |
1876 |
entry->skip = 1; |
entry->skip = 1; |
1897 |
|
|
1898 |
/* defaultImage now points to what we'd like to use, but before any order |
/* defaultImage now points to what we'd like to use, but before any order |
1899 |
changes */ |
changes */ |
1900 |
if (config->defaultImage == DEFAULT_SAVED) |
if ((config->defaultImage == DEFAULT_SAVED) || |
1901 |
|
(config->defaultImage == DEFAULT_SAVED_GRUB2)) |
1902 |
/* default is set to saved, we don't want to change it */ |
/* default is set to saved, we don't want to change it */ |
1903 |
return; |
return; |
1904 |
|
|
1965 |
return; |
return; |
1966 |
} |
} |
1967 |
|
|
1968 |
printf("kernel=%s\n", line->elements[1].item); |
printf("kernel=%s%s\n", prefix, line->elements[1].item); |
1969 |
|
|
1970 |
if (line->numElements >= 3) { |
if (line->numElements >= 3) { |
1971 |
printf("args=\""); |
printf("args=\""); |
2029 |
printf("%s%s", line->elements[i].item, line->elements[i].indent); |
printf("%s%s", line->elements[i].item, line->elements[i].indent); |
2030 |
printf("\n"); |
printf("\n"); |
2031 |
} |
} |
2032 |
|
|
2033 |
|
line = getLineByType(LT_TITLE, entry->lines); |
2034 |
|
if (line) { |
2035 |
|
printf("title=%s\n", line->elements[1].item); |
2036 |
|
} else { |
2037 |
|
char * title; |
2038 |
|
line = getLineByType(LT_MENUENTRY, entry->lines); |
2039 |
|
title = grub2ExtractTitle(line); |
2040 |
|
if (title) |
2041 |
|
printf("title=%s\n", title); |
2042 |
|
} |
2043 |
} |
} |
2044 |
|
|
2045 |
int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) { |
int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) { |
2294 |
free(line); |
free(line); |
2295 |
} |
} |
2296 |
|
|
|
static int isquote(char q) |
|
|
{ |
|
|
if (q == '\'' || q == '\"') |
|
|
return 1; |
|
|
return 0; |
|
|
} |
|
|
|
|
2297 |
static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi) |
static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi) |
2298 |
{ |
{ |
2299 |
struct singleLine newLine = { |
struct singleLine newLine = { |
2741 |
if (memcmp(boot, bootSect, 3)) |
if (memcmp(boot, bootSect, 3)) |
2742 |
return 0; |
return 0; |
2743 |
|
|
2744 |
if (boot[1] == 0xeb) { |
if (boot[1] == JMP_SHORT_OPCODE) { |
2745 |
offset = boot[2] + 2; |
offset = boot[2] + 2; |
2746 |
} else if (boot[1] == 0xe8 || boot[1] == 0xe9) { |
} else if (boot[1] == 0xe8 || boot[1] == 0xe9) { |
2747 |
offset = (boot[3] << 8) + boot[2] + 2; |
offset = (boot[3] << 8) + boot[2] + 2; |
2748 |
} else if (boot[0] == 0xeb) { |
} else if (boot[0] == JMP_SHORT_OPCODE) { |
2749 |
offset = boot[1] + 2; |
offset = boot[1] + 2; |
2750 |
|
/* |
2751 |
|
* it looks like grub, when copying stage1 into the mbr, patches stage1 |
2752 |
|
* right after the JMP location, replacing other instructions such as |
2753 |
|
* JMPs for NOOPs. So, relax the check a little bit by skipping those |
2754 |
|
* different bytes. |
2755 |
|
*/ |
2756 |
|
if ((bootSect[offset + 1] == NOOP_OPCODE) |
2757 |
|
&& (bootSect[offset + 2] == NOOP_OPCODE)) { |
2758 |
|
offset = offset + 3; |
2759 |
|
} |
2760 |
} else if (boot[0] == 0xe8 || boot[0] == 0xe9) { |
} else if (boot[0] == 0xe8 || boot[0] == 0xe9) { |
2761 |
offset = (boot[2] << 8) + boot[1] + 2; |
offset = (boot[2] << 8) + boot[1] + 2; |
2762 |
} else { |
} else { |
2899 |
} |
} |
2900 |
|
|
2901 |
int checkForGrub2(struct grubConfig * config) { |
int checkForGrub2(struct grubConfig * config) { |
2902 |
if (!access("/boot/grub2", R_OK)) |
if (!access("/etc/grub.d/", R_OK)) |
2903 |
return 2; |
return 2; |
2904 |
|
|
2905 |
return 1; |
return 1; |
3231 |
} |
} |
3232 |
} else if (tmplLine->type == LT_ECHO) { |
} else if (tmplLine->type == LT_ECHO) { |
3233 |
requote(tmplLine, config->cfi); |
requote(tmplLine, config->cfi); |
3234 |
|
static const char *prefix = "'Loading "; |
3235 |
if (tmplLine->numElements > 1 && |
if (tmplLine->numElements > 1 && |
3236 |
strstr(tmplLine->elements[1].item, "'Loading Linux ")) { |
strstr(tmplLine->elements[1].item, prefix) && |
3237 |
char *prefix = "'Loading "; |
masterLine->next && masterLine->next->type == LT_KERNEL) { |
3238 |
char *newTitle = malloc(strlen(prefix) + |
char *newTitle = malloc(strlen(prefix) + |
3239 |
strlen(newKernelTitle) + 2); |
strlen(newKernelTitle) + 2); |
3240 |
|
|
3431 |
struct singleEntry * template = NULL; |
struct singleEntry * template = NULL; |
3432 |
int copyDefault = 0, makeDefault = 0; |
int copyDefault = 0, makeDefault = 0; |
3433 |
int displayDefault = 0; |
int displayDefault = 0; |
3434 |
|
int displayDefaultIndex = 0; |
3435 |
|
int displayDefaultTitle = 0; |
3436 |
struct poptOption options[] = { |
struct poptOption options[] = { |
3437 |
{ "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0, |
{ "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0, |
3438 |
_("add an entry for the specified kernel"), _("kernel-path") }, |
_("add an entry for the specified kernel"), _("kernel-path") }, |
3463 |
"the kernel referenced by the default image does not exist, " |
"the kernel referenced by the default image does not exist, " |
3464 |
"the first linux entry whose kernel does exist is used as the " |
"the first linux entry whose kernel does exist is used as the " |
3465 |
"template"), NULL }, |
"template"), NULL }, |
3466 |
|
{ "debug", 0, 0, &debug, 0, |
3467 |
|
_("print debugging information for failures") }, |
3468 |
{ "default-kernel", 0, 0, &displayDefault, 0, |
{ "default-kernel", 0, 0, &displayDefault, 0, |
3469 |
_("display the path of the default kernel") }, |
_("display the path of the default kernel") }, |
3470 |
|
{ "default-index", 0, 0, &displayDefaultIndex, 0, |
3471 |
|
_("display the index of the default kernel") }, |
3472 |
|
{ "default-title", 0, 0, &displayDefaultTitle, 0, |
3473 |
|
_("display the title of the default kernel") }, |
3474 |
{ "elilo", 0, POPT_ARG_NONE, &configureELilo, 0, |
{ "elilo", 0, POPT_ARG_NONE, &configureELilo, 0, |
3475 |
_("configure elilo bootloader") }, |
_("configure elilo bootloader") }, |
3476 |
{ "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0, |
{ "extlinux", 0, POPT_ARG_NONE, &configureExtLinux, 0, |
3587 |
} |
} |
3588 |
|
|
3589 |
if (!cfi) { |
if (!cfi) { |
3590 |
|
if (grub2FindConfig(&grub2ConfigType)) |
3591 |
|
cfi = &grub2ConfigType; |
3592 |
|
else |
3593 |
#ifdef __ia64__ |
#ifdef __ia64__ |
3594 |
cfi = &eliloConfigType; |
cfi = &eliloConfigType; |
3595 |
#elif __powerpc__ |
#elif __powerpc__ |
3596 |
cfi = &yabootConfigType; |
cfi = &yabootConfigType; |
3597 |
#elif __sparc__ |
#elif __sparc__ |
3598 |
cfi = &siloConfigType; |
cfi = &siloConfigType; |
3599 |
#elif __s390__ |
#elif __s390__ |
3600 |
cfi = &ziplConfigType; |
cfi = &ziplConfigType; |
3601 |
#elif __s390x__ |
#elif __s390x__ |
3602 |
cfi = &ziplConfigtype; |
cfi = &ziplConfigtype; |
3603 |
#else |
#else |
|
if (grub2FindConfig(&grub2ConfigType)) |
|
|
cfi = &grub2ConfigType; |
|
|
else |
|
3604 |
cfi = &grubConfigType; |
cfi = &grubConfigType; |
3605 |
#endif |
#endif |
3606 |
} |
} |
3614 |
|
|
3615 |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
3616 |
newKernelPath || removeKernelPath || makeDefault || |
newKernelPath || removeKernelPath || makeDefault || |
3617 |
defaultKernel)) { |
defaultKernel || displayDefaultIndex || displayDefaultTitle)) { |
3618 |
fprintf(stderr, _("grubby: --bootloader-probe may not be used with " |
fprintf(stderr, _("grubby: --bootloader-probe may not be used with " |
3619 |
"specified option")); |
"specified option")); |
3620 |
return 1; |
return 1; |
3657 |
defaultKernel = NULL; |
defaultKernel = NULL; |
3658 |
} |
} |
3659 |
|
|
3660 |
if (!strcmp(grubConfig, "-") && !outputFile) { |
if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) { |
3661 |
fprintf(stderr, _("grubby: output file must be specified if stdin " |
fprintf(stderr, _("grubby: output file must be specified if stdin " |
3662 |
"is used\n")); |
"is used\n")); |
3663 |
return 1; |
return 1; |
3665 |
|
|
3666 |
if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel |
if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel |
3667 |
&& !kernelInfo && !bootloaderProbe && !updateKernelPath |
&& !kernelInfo && !bootloaderProbe && !updateKernelPath |
3668 |
&& !removeMBKernel) { |
&& !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) { |
3669 |
fprintf(stderr, _("grubby: no action specified\n")); |
fprintf(stderr, _("grubby: no action specified\n")); |
3670 |
return 1; |
return 1; |
3671 |
} |
} |
3704 |
gr2c = checkForGrub2(gconfig); |
gr2c = checkForGrub2(gconfig); |
3705 |
} |
} |
3706 |
|
|
3707 |
if (!access(grubConfigType.defaultConfig, F_OK)) { |
const char *grubconfig = grubFindConfig(&grubConfigType); |
3708 |
gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType); |
if (!access(grubconfig, F_OK)) { |
3709 |
|
gconfig = readConfig(grubconfig, &grubConfigType); |
3710 |
if (!gconfig) |
if (!gconfig) |
3711 |
grc = 1; |
grc = 1; |
3712 |
else |
else |
3760 |
((rootspec != NULL) ? strlen(rootspec) : 0)); |
((rootspec != NULL) ? strlen(rootspec) : 0)); |
3761 |
|
|
3762 |
return 0; |
return 0; |
3763 |
|
|
3764 |
|
} else if (displayDefaultTitle) { |
3765 |
|
struct singleLine * line; |
3766 |
|
struct singleEntry * entry; |
3767 |
|
|
3768 |
|
if (config->defaultImage == -1) return 0; |
3769 |
|
entry = findEntryByIndex(config, config->defaultImage); |
3770 |
|
if (!entry) return 0; |
3771 |
|
|
3772 |
|
if (!configureGrub2) { |
3773 |
|
line = getLineByType(LT_TITLE, entry->lines); |
3774 |
|
if (!line) return 0; |
3775 |
|
printf("%s\n", line->elements[1].item); |
3776 |
|
|
3777 |
|
} else { |
3778 |
|
char * title; |
3779 |
|
|
3780 |
|
dbgPrintf("This is GRUB2, default title is embeded in menuentry\n"); |
3781 |
|
line = getLineByType(LT_MENUENTRY, entry->lines); |
3782 |
|
if (!line) return 0; |
3783 |
|
title = grub2ExtractTitle(line); |
3784 |
|
if (title) |
3785 |
|
printf("%s\n", title); |
3786 |
|
} |
3787 |
|
return 0; |
3788 |
|
|
3789 |
|
} else if (displayDefaultIndex) { |
3790 |
|
if (config->defaultImage == -1) return 0; |
3791 |
|
printf("%i\n", config->defaultImage); |
3792 |
|
|
3793 |
} else if (kernelInfo) |
} else if (kernelInfo) |
3794 |
return displayInfo(config, kernelInfo, bootPrefix); |
return displayInfo(config, kernelInfo, bootPrefix); |
3795 |
|
|