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; |
164 |
|
|
165 |
const char *grubFindConfig(struct configFileInfo *cfi) { |
const char *grubFindConfig(struct configFileInfo *cfi) { |
166 |
static const char *configFiles[] = { |
static const char *configFiles[] = { |
|
"/etc/grub.conf", |
|
167 |
"/boot/grub/grub.conf", |
"/boot/grub/grub.conf", |
168 |
"/boot/grub/menu.lst", |
"/boot/grub/menu.lst", |
169 |
|
"/etc/grub.conf", |
170 |
NULL |
NULL |
171 |
}; |
}; |
172 |
static int i = -1; |
static int i = -1; |
205 |
{ "default", LT_DEFAULT, ' ' }, |
{ "default", LT_DEFAULT, ' ' }, |
206 |
{ "fallback", LT_FALLBACK, ' ' }, |
{ "fallback", LT_FALLBACK, ' ' }, |
207 |
{ "linux", LT_KERNEL, ' ' }, |
{ "linux", LT_KERNEL, ' ' }, |
208 |
|
{ "linuxefi", LT_KERNEL, ' ' }, |
209 |
{ "initrd", LT_INITRD, ' ', ' ' }, |
{ "initrd", LT_INITRD, ' ', ' ' }, |
210 |
|
{ "initrdefi", LT_INITRD, ' ', ' ' }, |
211 |
{ "module", LT_MBMODULE, ' ' }, |
{ "module", LT_MBMODULE, ' ' }, |
212 |
{ "kernel", LT_HYPER, ' ' }, |
{ "kernel", LT_HYPER, ' ' }, |
213 |
{ NULL, 0, 0 }, |
{ NULL, 0, 0 }, |
244 |
return configFiles[i]; |
return configFiles[i]; |
245 |
} |
} |
246 |
|
|
247 |
|
int sizeOfSingleLine(struct singleLine * line) { |
248 |
|
int count = 0; |
249 |
|
|
250 |
|
for (int i = 0; i < line->numElements; i++) { |
251 |
|
int indentSize = 0; |
252 |
|
|
253 |
|
count = count + strlen(line->elements[i].item); |
254 |
|
|
255 |
|
indentSize = strlen(line->elements[i].indent); |
256 |
|
if (indentSize > 0) |
257 |
|
count = count + indentSize; |
258 |
|
else |
259 |
|
/* be extra safe and add room for whitespaces */ |
260 |
|
count = count + 1; |
261 |
|
} |
262 |
|
|
263 |
|
/* room for trailing terminator */ |
264 |
|
count = count + 1; |
265 |
|
|
266 |
|
return count; |
267 |
|
} |
268 |
|
|
269 |
|
static int isquote(char q) |
270 |
|
{ |
271 |
|
if (q == '\'' || q == '\"') |
272 |
|
return 1; |
273 |
|
return 0; |
274 |
|
} |
275 |
|
|
276 |
|
char *grub2ExtractTitle(struct singleLine * line) { |
277 |
|
char * current; |
278 |
|
char * current_indent; |
279 |
|
int current_len; |
280 |
|
int current_indent_len; |
281 |
|
int i; |
282 |
|
|
283 |
|
/* bail out if line does not start with menuentry */ |
284 |
|
if (strcmp(line->elements[0].item, "menuentry")) |
285 |
|
return NULL; |
286 |
|
|
287 |
|
i = 1; |
288 |
|
current = line->elements[i].item; |
289 |
|
current_len = strlen(current); |
290 |
|
|
291 |
|
/* if second word is quoted, strip the quotes and return single word */ |
292 |
|
if (isquote(*current) && isquote(current[current_len - 1])) { |
293 |
|
char *tmp; |
294 |
|
|
295 |
|
tmp = strdup(current); |
296 |
|
*(tmp + current_len - 1) = '\0'; |
297 |
|
return ++tmp; |
298 |
|
} |
299 |
|
|
300 |
|
/* if no quotes, return second word verbatim */ |
301 |
|
if (!isquote(*current)) |
302 |
|
return current; |
303 |
|
|
304 |
|
/* second element start with a quote, so we have to find the element |
305 |
|
* whose last character is also quote (assuming it's the closing one) */ |
306 |
|
int resultMaxSize; |
307 |
|
char * result; |
308 |
|
|
309 |
|
resultMaxSize = sizeOfSingleLine(line); |
310 |
|
result = malloc(resultMaxSize); |
311 |
|
snprintf(result, resultMaxSize, "%s", ++current); |
312 |
|
|
313 |
|
i++; |
314 |
|
for (; i < line->numElements; ++i) { |
315 |
|
current = line->elements[i].item; |
316 |
|
current_len = strlen(current); |
317 |
|
current_indent = line->elements[i].indent; |
318 |
|
current_indent_len = strlen(current_indent); |
319 |
|
|
320 |
|
strncat(result, current_indent, current_indent_len); |
321 |
|
if (!isquote(current[current_len-1])) { |
322 |
|
strncat(result, current, current_len); |
323 |
|
} else { |
324 |
|
strncat(result, current, current_len - 1); |
325 |
|
break; |
326 |
|
} |
327 |
|
} |
328 |
|
return result; |
329 |
|
} |
330 |
|
|
331 |
struct configFileInfo grub2ConfigType = { |
struct configFileInfo grub2ConfigType = { |
332 |
.findConfig = grub2FindConfig, |
.findConfig = grub2FindConfig, |
333 |
.keywords = grub2Keywords, |
.keywords = grub2Keywords, |
334 |
.defaultIsIndex = 1, |
.defaultIsIndex = 1, |
335 |
.defaultSupportSaved = 0, |
.defaultSupportSaved = 1, |
336 |
.defaultIsVariable = 1, |
.defaultIsVariable = 1, |
337 |
.entryStart = LT_MENUENTRY, |
.entryStart = LT_MENUENTRY, |
338 |
.entryEnd = LT_ENTRY_END, |
.entryEnd = LT_ENTRY_END, |
574 |
|
|
575 |
static struct keywordTypes * getKeywordByType(enum lineType_e type, |
static struct keywordTypes * getKeywordByType(enum lineType_e type, |
576 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
577 |
struct keywordTypes * kw; |
for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) { |
|
for (kw = cfi->keywords; kw->key; kw++) { |
|
578 |
if (kw->type == type) |
if (kw->type == type) |
579 |
return kw; |
return kw; |
580 |
} |
} |
604 |
|
|
605 |
static enum lineType_e getTypeByKeyword(char * keyword, |
static enum lineType_e getTypeByKeyword(char * keyword, |
606 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
607 |
struct keywordTypes * kw; |
for (struct keywordTypes *kw = cfi->keywords; kw->key; kw++) { |
|
for (kw = cfi->keywords; kw->key; kw++) { |
|
608 |
if (!strcmp(keyword, kw->key)) |
if (!strcmp(keyword, kw->key)) |
609 |
return kw->type; |
return kw->type; |
610 |
} |
} |
691 |
} |
} |
692 |
|
|
693 |
struct singleLine * lineDup(struct singleLine * line) { |
struct singleLine * lineDup(struct singleLine * line) { |
|
int i; |
|
694 |
struct singleLine * newLine = malloc(sizeof(*newLine)); |
struct singleLine * newLine = malloc(sizeof(*newLine)); |
695 |
|
|
696 |
newLine->indent = strdup(line->indent); |
newLine->indent = strdup(line->indent); |
700 |
newLine->elements = malloc(sizeof(*newLine->elements) * |
newLine->elements = malloc(sizeof(*newLine->elements) * |
701 |
newLine->numElements); |
newLine->numElements); |
702 |
|
|
703 |
for (i = 0; i < newLine->numElements; i++) { |
for (int i = 0; i < newLine->numElements; i++) { |
704 |
newLine->elements[i].indent = strdup(line->elements[i].indent); |
newLine->elements[i].indent = strdup(line->elements[i].indent); |
705 |
newLine->elements[i].item = strdup(line->elements[i].item); |
newLine->elements[i].item = strdup(line->elements[i].item); |
706 |
} |
} |
709 |
} |
} |
710 |
|
|
711 |
static void lineFree(struct singleLine * line) { |
static void lineFree(struct singleLine * line) { |
|
int i; |
|
|
|
|
712 |
if (line->indent) free(line->indent); |
if (line->indent) free(line->indent); |
713 |
|
|
714 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
715 |
free(line->elements[i].item); |
free(line->elements[i].item); |
716 |
free(line->elements[i].indent); |
free(line->elements[i].indent); |
717 |
} |
} |
722 |
|
|
723 |
static int lineWrite(FILE * out, struct singleLine * line, |
static int lineWrite(FILE * out, struct singleLine * line, |
724 |
struct configFileInfo * cfi) { |
struct configFileInfo * cfi) { |
|
int i; |
|
|
|
|
725 |
if (fprintf(out, "%s", line->indent) == -1) return -1; |
if (fprintf(out, "%s", line->indent) == -1) return -1; |
726 |
|
|
727 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
728 |
|
/* Need to handle this, because we strip the quotes from |
729 |
|
* menuentry when read it. */ |
730 |
|
if (line->type == LT_MENUENTRY && i == 1) { |
731 |
|
if(!isquote(*line->elements[i].item)) |
732 |
|
fprintf(out, "\'%s\'", line->elements[i].item); |
733 |
|
else |
734 |
|
fprintf(out, "%s", line->elements[i].item); |
735 |
|
fprintf(out, "%s", line->elements[i].indent); |
736 |
|
|
737 |
|
continue; |
738 |
|
} |
739 |
|
|
740 |
if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes) |
if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes) |
741 |
if (fputc('"', out) == EOF) return -1; |
if (fputc('"', out) == EOF) return -1; |
742 |
|
|
830 |
if (*line->elements[0].item == '#') { |
if (*line->elements[0].item == '#') { |
831 |
char * fullLine; |
char * fullLine; |
832 |
int len; |
int len; |
|
int i; |
|
833 |
|
|
834 |
len = strlen(line->indent); |
len = strlen(line->indent); |
835 |
for (i = 0; i < line->numElements; i++) |
for (int i = 0; i < line->numElements; i++) |
836 |
len += strlen(line->elements[i].item) + |
len += strlen(line->elements[i].item) + |
837 |
strlen(line->elements[i].indent); |
strlen(line->elements[i].indent); |
838 |
|
|
841 |
free(line->indent); |
free(line->indent); |
842 |
line->indent = fullLine; |
line->indent = fullLine; |
843 |
|
|
844 |
for (i = 0; i < line->numElements; i++) { |
for (int i = 0; i < line->numElements; i++) { |
845 |
strcat(fullLine, line->elements[i].item); |
strcat(fullLine, line->elements[i].item); |
846 |
strcat(fullLine, line->elements[i].indent); |
strcat(fullLine, line->elements[i].indent); |
847 |
free(line->elements[i].item); |
free(line->elements[i].item); |
860 |
* elements up more |
* elements up more |
861 |
*/ |
*/ |
862 |
if (!isspace(kw->separatorChar)) { |
if (!isspace(kw->separatorChar)) { |
|
int i; |
|
863 |
char indent[2] = ""; |
char indent[2] = ""; |
864 |
indent[0] = kw->separatorChar; |
indent[0] = kw->separatorChar; |
865 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
866 |
char *p; |
char *p; |
|
int j; |
|
867 |
int numNewElements; |
int numNewElements; |
868 |
|
|
869 |
numNewElements = 0; |
numNewElements = 0; |
879 |
sizeof(*line->elements) * elementsAlloced); |
sizeof(*line->elements) * elementsAlloced); |
880 |
} |
} |
881 |
|
|
882 |
for (j = line->numElements; j > i; j--) { |
for (int j = line->numElements; j > i; j--) { |
883 |
line->elements[j + numNewElements] = line->elements[j]; |
line->elements[j + numNewElements] = line->elements[j]; |
884 |
} |
} |
885 |
line->numElements += numNewElements; |
line->numElements += numNewElements; |
892 |
break; |
break; |
893 |
} |
} |
894 |
|
|
895 |
free(line->elements[i].indent); |
line->elements[i + 1].indent = line->elements[i].indent; |
896 |
line->elements[i].indent = strdup(indent); |
line->elements[i].indent = strdup(indent); |
897 |
*p++ = '\0'; |
*p++ = '\0'; |
898 |
i++; |
i++; |
899 |
line->elements[i].item = strdup(p); |
line->elements[i].item = strdup(p); |
|
line->elements[i].indent = strdup(""); |
|
|
p = line->elements[i].item; |
|
900 |
} |
} |
901 |
} |
} |
902 |
} |
} |
917 |
struct singleLine * last = NULL, * line, * defaultLine = NULL; |
struct singleLine * last = NULL, * line, * defaultLine = NULL; |
918 |
char * end; |
char * end; |
919 |
struct singleEntry * entry = NULL; |
struct singleEntry * entry = NULL; |
920 |
int i, len; |
int len; |
921 |
char * buf; |
char * buf; |
922 |
|
|
923 |
if (!strcmp(inName, "-")) { |
if (!strcmp(inName, "-")) { |
980 |
} |
} |
981 |
|
|
982 |
if (line->type == LT_SET_VARIABLE) { |
if (line->type == LT_SET_VARIABLE) { |
|
int i; |
|
983 |
dbgPrintf("found 'set' command (%d elements): ", line->numElements); |
dbgPrintf("found 'set' command (%d elements): ", line->numElements); |
984 |
dbgPrintf("%s", line->indent); |
dbgPrintf("%s", line->indent); |
985 |
for (i = 0; i < line->numElements; i++) |
for (int i = 0; i < line->numElements; i++) |
986 |
dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item); |
dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent); |
987 |
dbgPrintf("\n"); |
dbgPrintf("\n"); |
988 |
struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi); |
struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi); |
989 |
if (kwType && line->numElements == 3 && |
if (kwType && line->numElements == 3 && |
1010 |
* This only applies to grub, but that's the only place we |
* This only applies to grub, but that's the only place we |
1011 |
* should find LT_MBMODULE lines anyway. |
* should find LT_MBMODULE lines anyway. |
1012 |
*/ |
*/ |
1013 |
struct singleLine * l; |
for (struct singleLine *l = entry->lines; l; l = l->next) { |
|
for (l = entry->lines; l; l = l->next) { |
|
1014 |
if (l->type == LT_HYPER) |
if (l->type == LT_HYPER) |
1015 |
break; |
break; |
1016 |
else if (l->type == LT_KERNEL) { |
else if (l->type == LT_KERNEL) { |
1030 |
} else if (line->type == LT_TITLE && line->numElements > 1) { |
} else if (line->type == LT_TITLE && line->numElements > 1) { |
1031 |
/* make the title a single argument (undoing our parsing) */ |
/* make the title a single argument (undoing our parsing) */ |
1032 |
len = 0; |
len = 0; |
1033 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
1034 |
len += strlen(line->elements[i].item); |
len += strlen(line->elements[i].item); |
1035 |
len += strlen(line->elements[i].indent); |
len += strlen(line->elements[i].indent); |
1036 |
} |
} |
1037 |
buf = malloc(len + 1); |
buf = malloc(len + 1); |
1038 |
*buf = '\0'; |
*buf = '\0'; |
1039 |
|
|
1040 |
for (i = 1; i < line->numElements; i++) { |
for (int i = 1; i < line->numElements; i++) { |
1041 |
strcat(buf, line->elements[i].item); |
strcat(buf, line->elements[i].item); |
1042 |
free(line->elements[i].item); |
free(line->elements[i].item); |
1043 |
|
|
1051 |
line->elements[line->numElements - 1].indent; |
line->elements[line->numElements - 1].indent; |
1052 |
line->elements[1].item = buf; |
line->elements[1].item = buf; |
1053 |
line->numElements = 2; |
line->numElements = 2; |
1054 |
|
} else if (line->type == LT_MENUENTRY && line->numElements > 3) { |
1055 |
|
/* let --remove-kernel="TITLE=what" work */ |
1056 |
|
len = 0; |
1057 |
|
char *extras; |
1058 |
|
char *title; |
1059 |
|
|
1060 |
|
for (int i = 1; i < line->numElements; i++) { |
1061 |
|
len += strlen(line->elements[i].item); |
1062 |
|
len += strlen(line->elements[i].indent); |
1063 |
|
} |
1064 |
|
buf = malloc(len + 1); |
1065 |
|
*buf = '\0'; |
1066 |
|
|
1067 |
|
/* allocate mem for extra flags. */ |
1068 |
|
extras = malloc(len + 1); |
1069 |
|
*extras = '\0'; |
1070 |
|
|
1071 |
|
/* get title. */ |
1072 |
|
for (int i = 0; i < line->numElements; i++) { |
1073 |
|
if (!strcmp(line->elements[i].item, "menuentry")) |
1074 |
|
continue; |
1075 |
|
if (isquote(*line->elements[i].item)) |
1076 |
|
title = line->elements[i].item + 1; |
1077 |
|
else |
1078 |
|
title = line->elements[i].item; |
1079 |
|
|
1080 |
|
len = strlen(title); |
1081 |
|
if (isquote(title[len-1])) { |
1082 |
|
strncat(buf, title,len-1); |
1083 |
|
break; |
1084 |
|
} else { |
1085 |
|
strcat(buf, title); |
1086 |
|
strcat(buf, line->elements[i].indent); |
1087 |
|
} |
1088 |
|
} |
1089 |
|
|
1090 |
|
/* get extras */ |
1091 |
|
int count = 0; |
1092 |
|
for (int i = 0; i < line->numElements; i++) { |
1093 |
|
if (count >= 2) { |
1094 |
|
strcat(extras, line->elements[i].item); |
1095 |
|
strcat(extras, line->elements[i].indent); |
1096 |
|
} |
1097 |
|
|
1098 |
|
if (!strcmp(line->elements[i].item, "menuentry")) |
1099 |
|
continue; |
1100 |
|
|
1101 |
|
/* count ' or ", there should be two in menuentry line. */ |
1102 |
|
if (isquote(*line->elements[i].item)) |
1103 |
|
count++; |
1104 |
|
|
1105 |
|
len = strlen(line->elements[i].item); |
1106 |
|
|
1107 |
|
if (isquote(line->elements[i].item[len -1])) |
1108 |
|
count++; |
1109 |
|
|
1110 |
|
/* ok, we get the final ' or ", others are extras. */ |
1111 |
|
} |
1112 |
|
line->elements[1].indent = |
1113 |
|
line->elements[line->numElements - 2].indent; |
1114 |
|
line->elements[1].item = buf; |
1115 |
|
line->elements[2].indent = |
1116 |
|
line->elements[line->numElements - 2].indent; |
1117 |
|
line->elements[2].item = extras; |
1118 |
|
line->numElements = 3; |
1119 |
} else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) { |
} else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) { |
1120 |
/* Strip off any " which may be present; they'll be put back |
/* Strip off any " which may be present; they'll be put back |
1121 |
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 |
1124 |
if (line->numElements >= 2) { |
if (line->numElements >= 2) { |
1125 |
int last, len; |
int last, len; |
1126 |
|
|
1127 |
if (*line->elements[1].item == '"') |
if (isquote(*line->elements[1].item)) |
1128 |
memmove(line->elements[1].item, line->elements[1].item + 1, |
memmove(line->elements[1].item, line->elements[1].item + 1, |
1129 |
strlen(line->elements[1].item + 1) + 1); |
strlen(line->elements[1].item + 1) + 1); |
1130 |
|
|
1131 |
last = line->numElements - 1; |
last = line->numElements - 1; |
1132 |
len = strlen(line->elements[last].item) - 1; |
len = strlen(line->elements[last].item) - 1; |
1133 |
if (line->elements[last].item[len] == '"') |
if (isquote(line->elements[last].item[len])) |
1134 |
line->elements[last].item[len] = '\0'; |
line->elements[last].item[len] = '\0'; |
1135 |
} |
} |
1136 |
} |
} |
1189 |
|
|
1190 |
dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset"); |
dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset"); |
1191 |
if (defaultLine) { |
if (defaultLine) { |
1192 |
if (cfi->defaultIsVariable) { |
if (defaultLine->numElements > 2 && |
1193 |
|
cfi->defaultSupportSaved && |
1194 |
|
!strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) { |
1195 |
|
cfg->defaultImage = DEFAULT_SAVED_GRUB2; |
1196 |
|
} else if (cfi->defaultIsVariable) { |
1197 |
char *value = defaultLine->elements[2].item; |
char *value = defaultLine->elements[2].item; |
1198 |
while (*value && (*value == '"' || *value == '\'' || |
while (*value && (*value == '"' || *value == '\'' || |
1199 |
*value == ' ' || *value == '\t')) |
*value == ' ' || *value == '\t')) |
1210 |
cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10); |
cfg->defaultImage = strtol(defaultLine->elements[1].item, &end, 10); |
1211 |
if (*end) cfg->defaultImage = -1; |
if (*end) cfg->defaultImage = -1; |
1212 |
} else if (defaultLine->numElements >= 2) { |
} else if (defaultLine->numElements >= 2) { |
1213 |
i = 0; |
int i = 0; |
1214 |
while ((entry = findEntryByIndex(cfg, i))) { |
while ((entry = findEntryByIndex(cfg, i))) { |
1215 |
for (line = entry->lines; line; line = line->next) |
for (line = entry->lines; line; line = line->next) |
1216 |
if (line->type == LT_TITLE) break; |
if (line->type == LT_TITLE) break; |
1250 |
|
|
1251 |
if (cfg->defaultImage == DEFAULT_SAVED) |
if (cfg->defaultImage == DEFAULT_SAVED) |
1252 |
fprintf(out, "%sdefault%ssaved\n", indent, separator); |
fprintf(out, "%sdefault%ssaved\n", indent, separator); |
1253 |
|
else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2) |
1254 |
|
fprintf(out, "%sset default=\"${saved_entry}\"\n", indent); |
1255 |
else if (cfg->defaultImage > -1) { |
else if (cfg->defaultImage > -1) { |
1256 |
if (cfg->cfi->defaultIsIndex) { |
if (cfg->cfi->defaultIsIndex) { |
1257 |
if (cfg->cfi->defaultIsVariable) { |
if (cfg->cfi->defaultIsVariable) { |
1312 |
|
|
1313 |
/* most likely the symlink is relative, so change our |
/* most likely the symlink is relative, so change our |
1314 |
directory to the dir of the symlink */ |
directory to the dir of the symlink */ |
1315 |
rc = chdir(dirname(strdupa(outName))); |
char *dir = strdupa(outName); |
1316 |
|
rc = chdir(dirname(dir)); |
1317 |
|
free(dir); |
1318 |
do { |
do { |
1319 |
buf = alloca(len + 1); |
buf = alloca(len + 1); |
1320 |
rc = readlink(basename(outName), buf, len); |
rc = readlink(basename(outName), buf, len); |
1452 |
buf[rc] = '\0'; |
buf[rc] = '\0'; |
1453 |
chptr = buf; |
chptr = buf; |
1454 |
|
|
1455 |
|
char *foundanswer = NULL; |
1456 |
|
|
1457 |
while (chptr && chptr != buf+rc) { |
while (chptr && chptr != buf+rc) { |
1458 |
devname = chptr; |
devname = chptr; |
1459 |
|
|
1481 |
* for '/' obviously. |
* for '/' obviously. |
1482 |
*/ |
*/ |
1483 |
if (*(++chptr) == '/' && *(++chptr) == ' ') { |
if (*(++chptr) == '/' && *(++chptr) == ' ') { |
1484 |
/* |
/* remember the last / entry in mtab */ |
1485 |
* Move back 2, which is the first space after the device name, set |
foundanswer = devname; |
|
* it to \0 so strdup will just get the devicename. |
|
|
*/ |
|
|
chptr -= 2; |
|
|
*chptr = '\0'; |
|
|
return strdup(devname); |
|
1486 |
} |
} |
1487 |
|
|
1488 |
/* Next line */ |
/* Next line */ |
1491 |
chptr++; |
chptr++; |
1492 |
} |
} |
1493 |
|
|
1494 |
|
/* Return the last / entry found */ |
1495 |
|
if (foundanswer) { |
1496 |
|
chptr = strchr(foundanswer, ' '); |
1497 |
|
*chptr = '\0'; |
1498 |
|
return strdup(foundanswer); |
1499 |
|
} |
1500 |
|
|
1501 |
return NULL; |
return NULL; |
1502 |
} |
} |
1503 |
|
|
1508 |
for (line = entry->lines; line; line = line->next) { |
for (line = entry->lines; line; line = line->next) { |
1509 |
fprintf(stderr, "DBG: %s", line->indent); |
fprintf(stderr, "DBG: %s", line->indent); |
1510 |
for (i = 0; i < line->numElements; i++) { |
for (i = 0; i < line->numElements; i++) { |
1511 |
fprintf(stderr, "%s%s", |
/* Need to handle this, because we strip the quotes from |
1512 |
|
* menuentry when read it. */ |
1513 |
|
if (line->type == LT_MENUENTRY && i == 1) { |
1514 |
|
if(!isquote(*line->elements[i].item)) |
1515 |
|
fprintf(stderr, "\'%s\'", line->elements[i].item); |
1516 |
|
else |
1517 |
|
fprintf(stderr, "%s", line->elements[i].item); |
1518 |
|
fprintf(stderr, "%s", line->elements[i].indent); |
1519 |
|
|
1520 |
|
continue; |
1521 |
|
} |
1522 |
|
|
1523 |
|
fprintf(stderr, "%s%s", |
1524 |
line->elements[i].item, line->elements[i].indent); |
line->elements[i].item, line->elements[i].indent); |
1525 |
} |
} |
1526 |
fprintf(stderr, "\n"); |
fprintf(stderr, "\n"); |
1547 |
{ |
{ |
1548 |
int slen; |
int slen; |
1549 |
|
|
1550 |
if (!s && !s[0]) |
if (!s || !s[0]) |
1551 |
return 0; |
return 0; |
1552 |
slen = strlen(s) - 1; |
slen = strlen(s) - 1; |
1553 |
|
|
1736 |
|
|
1737 |
if (!strncmp(kernel, "TITLE=", 6)) { |
if (!strncmp(kernel, "TITLE=", 6)) { |
1738 |
prefix = ""; |
prefix = ""; |
1739 |
checkType = LT_TITLE; |
checkType = LT_TITLE|LT_MENUENTRY; |
1740 |
kernel += 6; |
kernel += 6; |
1741 |
} |
} |
1742 |
|
|
1752 |
checkType, line); |
checkType, line); |
1753 |
if (!line) break; /* not found in this entry */ |
if (!line) break; /* not found in this entry */ |
1754 |
|
|
1755 |
if (line && line->numElements >= 2) { |
if (line && line->type != LT_MENUENTRY && |
1756 |
|
line->numElements >= 2) { |
1757 |
rootspec = getRootSpecifier(line->elements[1].item); |
rootspec = getRootSpecifier(line->elements[1].item); |
1758 |
if (!strcmp(line->elements[1].item + |
if (!strcmp(line->elements[1].item + |
1759 |
((rootspec != NULL) ? strlen(rootspec) : 0), |
((rootspec != NULL) ? strlen(rootspec) : 0), |
1760 |
kernel + strlen(prefix))) |
kernel + strlen(prefix))) |
1761 |
break; |
break; |
1762 |
} |
} |
1763 |
|
if(line->type == LT_MENUENTRY && |
1764 |
|
!strcmp(line->elements[1].item, kernel)) |
1765 |
|
break; |
1766 |
} |
} |
1767 |
|
|
1768 |
/* make sure this entry has a kernel identifier; this skips |
/* make sure this entry has a kernel identifier; this skips |
1854 |
const char * prefix) { |
const char * prefix) { |
1855 |
struct singleEntry * entry; |
struct singleEntry * entry; |
1856 |
|
|
1857 |
if (!image) return; |
if (!image) |
1858 |
|
return; |
1859 |
|
|
1860 |
|
/* check and see if we're removing the default image */ |
1861 |
|
if (isdigit(*image)) { |
1862 |
|
entry = findEntryByPath(cfg, image, prefix, NULL); |
1863 |
|
if(entry) |
1864 |
|
entry->skip = 1; |
1865 |
|
return; |
1866 |
|
} |
1867 |
|
|
1868 |
while ((entry = findEntryByPath(cfg, image, prefix, NULL))) |
while ((entry = findEntryByPath(cfg, image, prefix, NULL))) |
1869 |
entry->skip = 1; |
entry->skip = 1; |
1871 |
|
|
1872 |
void setDefaultImage(struct grubConfig * config, int hasNew, |
void setDefaultImage(struct grubConfig * config, int hasNew, |
1873 |
const char * defaultKernelPath, int newIsDefault, |
const char * defaultKernelPath, int newIsDefault, |
1874 |
const char * prefix, int flags) { |
const char * prefix, int flags, int index) { |
1875 |
struct singleEntry * entry, * entry2, * newDefault; |
struct singleEntry * entry, * entry2, * newDefault; |
1876 |
int i, j; |
int i, j; |
1877 |
|
|
1878 |
if (newIsDefault) { |
if (newIsDefault) { |
1879 |
config->defaultImage = 0; |
config->defaultImage = 0; |
1880 |
return; |
return; |
1881 |
|
} else if ((index >= 0) && config->cfi->defaultIsIndex) { |
1882 |
|
if (findEntryByIndex(config, index)) |
1883 |
|
config->defaultImage = index; |
1884 |
|
else |
1885 |
|
config->defaultImage = -1; |
1886 |
|
return; |
1887 |
} else if (defaultKernelPath) { |
} else if (defaultKernelPath) { |
1888 |
i = 0; |
i = 0; |
1889 |
if (findEntryByPath(config, defaultKernelPath, prefix, &i)) { |
if (findEntryByPath(config, defaultKernelPath, prefix, &i)) { |
1896 |
|
|
1897 |
/* 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 |
1898 |
changes */ |
changes */ |
1899 |
if (config->defaultImage == DEFAULT_SAVED) |
if ((config->defaultImage == DEFAULT_SAVED) || |
1900 |
|
(config->defaultImage == DEFAULT_SAVED_GRUB2)) |
1901 |
/* default is set to saved, we don't want to change it */ |
/* default is set to saved, we don't want to change it */ |
1902 |
return; |
return; |
1903 |
|
|
1964 |
return; |
return; |
1965 |
} |
} |
1966 |
|
|
1967 |
printf("kernel=%s\n", line->elements[1].item); |
if (!strncmp(prefix, line->elements[1].item, strlen(prefix))) |
1968 |
|
printf("kernel=%s\n", line->elements[1].item); |
1969 |
|
else |
1970 |
|
printf("kernel=%s%s\n", prefix, line->elements[1].item); |
1971 |
|
|
1972 |
if (line->numElements >= 3) { |
if (line->numElements >= 3) { |
1973 |
printf("args=\""); |
printf("args=\""); |
2026 |
line = getLineByType(LT_INITRD, entry->lines); |
line = getLineByType(LT_INITRD, entry->lines); |
2027 |
|
|
2028 |
if (line && line->numElements >= 2) { |
if (line && line->numElements >= 2) { |
2029 |
printf("initrd=%s", prefix); |
if (!strncmp(prefix, line->elements[1].item, strlen(prefix))) |
2030 |
|
printf("initrd="); |
2031 |
|
else |
2032 |
|
printf("initrd=%s", prefix); |
2033 |
|
|
2034 |
for (i = 1; i < line->numElements; i++) |
for (i = 1; i < line->numElements; i++) |
2035 |
printf("%s%s", line->elements[i].item, line->elements[i].indent); |
printf("%s%s", line->elements[i].item, line->elements[i].indent); |
2036 |
printf("\n"); |
printf("\n"); |
2037 |
} |
} |
2038 |
|
|
2039 |
|
line = getLineByType(LT_TITLE, entry->lines); |
2040 |
|
if (line) { |
2041 |
|
printf("title=%s\n", line->elements[1].item); |
2042 |
|
} else { |
2043 |
|
char * title; |
2044 |
|
line = getLineByType(LT_MENUENTRY, entry->lines); |
2045 |
|
title = grub2ExtractTitle(line); |
2046 |
|
if (title) |
2047 |
|
printf("title=%s\n", title); |
2048 |
|
} |
2049 |
|
} |
2050 |
|
|
2051 |
|
int isSuseSystem(void) { |
2052 |
|
const char * path; |
2053 |
|
const static char default_path[] = "/etc/SuSE-release"; |
2054 |
|
|
2055 |
|
if ((path = getenv("GRUBBY_SUSE_RELEASE")) == NULL) |
2056 |
|
path = default_path; |
2057 |
|
|
2058 |
|
if (!access(path, R_OK)) |
2059 |
|
return 1; |
2060 |
|
return 0; |
2061 |
|
} |
2062 |
|
|
2063 |
|
int isSuseGrubConf(const char * path) { |
2064 |
|
FILE * grubConf; |
2065 |
|
char * line = NULL; |
2066 |
|
size_t len = 0, res = 0; |
2067 |
|
|
2068 |
|
grubConf = fopen(path, "r"); |
2069 |
|
if (!grubConf) { |
2070 |
|
dbgPrintf("Could not open SuSE configuration file '%s'\n", path); |
2071 |
|
return 0; |
2072 |
|
} |
2073 |
|
|
2074 |
|
while ((res = getline(&line, &len, grubConf)) != -1) { |
2075 |
|
if (!strncmp(line, "setup", 5)) { |
2076 |
|
fclose(grubConf); |
2077 |
|
free(line); |
2078 |
|
return 1; |
2079 |
|
} |
2080 |
|
} |
2081 |
|
|
2082 |
|
dbgPrintf("SuSE configuration file '%s' does not appear to be valid\n", |
2083 |
|
path); |
2084 |
|
|
2085 |
|
fclose(grubConf); |
2086 |
|
free(line); |
2087 |
|
return 0; |
2088 |
|
} |
2089 |
|
|
2090 |
|
int suseGrubConfGetLba(const char * path, int * lbaPtr) { |
2091 |
|
FILE * grubConf; |
2092 |
|
char * line = NULL; |
2093 |
|
size_t res = 0, len = 0; |
2094 |
|
|
2095 |
|
if (!path) return 1; |
2096 |
|
if (!lbaPtr) return 1; |
2097 |
|
|
2098 |
|
grubConf = fopen(path, "r"); |
2099 |
|
if (!grubConf) return 1; |
2100 |
|
|
2101 |
|
while ((res = getline(&line, &len, grubConf)) != -1) { |
2102 |
|
if (line[res - 1] == '\n') |
2103 |
|
line[res - 1] = '\0'; |
2104 |
|
else if (len > res) |
2105 |
|
line[res] = '\0'; |
2106 |
|
else { |
2107 |
|
line = realloc(line, res + 1); |
2108 |
|
line[res] = '\0'; |
2109 |
|
} |
2110 |
|
|
2111 |
|
if (!strncmp(line, "setup", 5)) { |
2112 |
|
if (strstr(line, "--force-lba")) { |
2113 |
|
*lbaPtr = 1; |
2114 |
|
} else { |
2115 |
|
*lbaPtr = 0; |
2116 |
|
} |
2117 |
|
dbgPrintf("lba: %i\n", *lbaPtr); |
2118 |
|
break; |
2119 |
|
} |
2120 |
|
} |
2121 |
|
|
2122 |
|
free(line); |
2123 |
|
fclose(grubConf); |
2124 |
|
return 0; |
2125 |
|
} |
2126 |
|
|
2127 |
|
int suseGrubConfGetInstallDevice(const char * path, char ** devicePtr) { |
2128 |
|
FILE * grubConf; |
2129 |
|
char * line = NULL; |
2130 |
|
size_t res = 0, len = 0; |
2131 |
|
char * lastParamPtr = NULL; |
2132 |
|
char * secLastParamPtr = NULL; |
2133 |
|
char installDeviceNumber = '\0'; |
2134 |
|
char * bounds = NULL; |
2135 |
|
|
2136 |
|
if (!path) return 1; |
2137 |
|
if (!devicePtr) return 1; |
2138 |
|
|
2139 |
|
grubConf = fopen(path, "r"); |
2140 |
|
if (!grubConf) return 1; |
2141 |
|
|
2142 |
|
while ((res = getline(&line, &len, grubConf)) != -1) { |
2143 |
|
if (strncmp(line, "setup", 5)) |
2144 |
|
continue; |
2145 |
|
|
2146 |
|
if (line[res - 1] == '\n') |
2147 |
|
line[res - 1] = '\0'; |
2148 |
|
else if (len > res) |
2149 |
|
line[res] = '\0'; |
2150 |
|
else { |
2151 |
|
line = realloc(line, res + 1); |
2152 |
|
line[res] = '\0'; |
2153 |
|
} |
2154 |
|
|
2155 |
|
lastParamPtr = bounds = line + res; |
2156 |
|
|
2157 |
|
/* Last parameter in grub may be an optional IMAGE_DEVICE */ |
2158 |
|
while (!isspace(*lastParamPtr)) |
2159 |
|
lastParamPtr--; |
2160 |
|
lastParamPtr++; |
2161 |
|
|
2162 |
|
secLastParamPtr = lastParamPtr - 2; |
2163 |
|
dbgPrintf("lastParamPtr: %s\n", lastParamPtr); |
2164 |
|
|
2165 |
|
if (lastParamPtr + 3 > bounds) { |
2166 |
|
dbgPrintf("lastParamPtr going over boundary"); |
2167 |
|
fclose(grubConf); |
2168 |
|
free(line); |
2169 |
|
return 1; |
2170 |
|
} |
2171 |
|
if (!strncmp(lastParamPtr, "(hd", 3)) |
2172 |
|
lastParamPtr += 3; |
2173 |
|
dbgPrintf("lastParamPtr: %c\n", *lastParamPtr); |
2174 |
|
|
2175 |
|
/* |
2176 |
|
* Second last parameter will decide wether last parameter is |
2177 |
|
* an IMAGE_DEVICE or INSTALL_DEVICE |
2178 |
|
*/ |
2179 |
|
while (!isspace(*secLastParamPtr)) |
2180 |
|
secLastParamPtr--; |
2181 |
|
secLastParamPtr++; |
2182 |
|
|
2183 |
|
if (secLastParamPtr + 3 > bounds) { |
2184 |
|
dbgPrintf("secLastParamPtr going over boundary"); |
2185 |
|
fclose(grubConf); |
2186 |
|
free(line); |
2187 |
|
return 1; |
2188 |
|
} |
2189 |
|
dbgPrintf("secLastParamPtr: %s\n", secLastParamPtr); |
2190 |
|
if (!strncmp(secLastParamPtr, "(hd", 3)) { |
2191 |
|
secLastParamPtr += 3; |
2192 |
|
dbgPrintf("secLastParamPtr: %c\n", *secLastParamPtr); |
2193 |
|
installDeviceNumber = *secLastParamPtr; |
2194 |
|
} else { |
2195 |
|
installDeviceNumber = *lastParamPtr; |
2196 |
|
} |
2197 |
|
|
2198 |
|
*devicePtr = malloc(6); |
2199 |
|
snprintf(*devicePtr, 6, "(hd%c)", installDeviceNumber); |
2200 |
|
dbgPrintf("installDeviceNumber: %c\n", installDeviceNumber); |
2201 |
|
fclose(grubConf); |
2202 |
|
free(line); |
2203 |
|
return 0; |
2204 |
|
} |
2205 |
|
|
2206 |
|
free(line); |
2207 |
|
fclose(grubConf); |
2208 |
|
return 1; |
2209 |
|
} |
2210 |
|
|
2211 |
|
int grubGetBootFromDeviceMap(const char * device, |
2212 |
|
char ** bootPtr) { |
2213 |
|
FILE * deviceMap; |
2214 |
|
char * line = NULL; |
2215 |
|
size_t res = 0, len = 0; |
2216 |
|
char * devicePtr; |
2217 |
|
char * bounds = NULL; |
2218 |
|
const char * path; |
2219 |
|
const static char default_path[] = "/boot/grub/device.map"; |
2220 |
|
|
2221 |
|
if (!device) return 1; |
2222 |
|
if (!bootPtr) return 1; |
2223 |
|
|
2224 |
|
if ((path = getenv("GRUBBY_GRUB_DEVICE_MAP")) == NULL) |
2225 |
|
path = default_path; |
2226 |
|
|
2227 |
|
dbgPrintf("opening grub device.map file from: %s\n", path); |
2228 |
|
deviceMap = fopen(path, "r"); |
2229 |
|
if (!deviceMap) |
2230 |
|
return 1; |
2231 |
|
|
2232 |
|
while ((res = getline(&line, &len, deviceMap)) != -1) { |
2233 |
|
if (!strncmp(line, "#", 1)) |
2234 |
|
continue; |
2235 |
|
|
2236 |
|
if (line[res - 1] == '\n') |
2237 |
|
line[res - 1] = '\0'; |
2238 |
|
else if (len > res) |
2239 |
|
line[res] = '\0'; |
2240 |
|
else { |
2241 |
|
line = realloc(line, res + 1); |
2242 |
|
line[res] = '\0'; |
2243 |
|
} |
2244 |
|
|
2245 |
|
devicePtr = line; |
2246 |
|
bounds = line + res; |
2247 |
|
|
2248 |
|
while ((isspace(*line) && ((devicePtr + 1) <= bounds))) |
2249 |
|
devicePtr++; |
2250 |
|
dbgPrintf("device: %s\n", devicePtr); |
2251 |
|
|
2252 |
|
if (!strncmp(devicePtr, device, strlen(device))) { |
2253 |
|
devicePtr += strlen(device); |
2254 |
|
while (isspace(*devicePtr) && ((devicePtr + 1) <= bounds)) |
2255 |
|
devicePtr++; |
2256 |
|
|
2257 |
|
*bootPtr = strdup(devicePtr); |
2258 |
|
break; |
2259 |
|
} |
2260 |
|
} |
2261 |
|
|
2262 |
|
free(line); |
2263 |
|
fclose(deviceMap); |
2264 |
|
return 0; |
2265 |
|
} |
2266 |
|
|
2267 |
|
int suseGrubConfGetBoot(const char * path, char ** bootPtr) { |
2268 |
|
char * grubDevice; |
2269 |
|
|
2270 |
|
if (suseGrubConfGetInstallDevice(path, &grubDevice)) |
2271 |
|
dbgPrintf("error looking for grub installation device\n"); |
2272 |
|
else |
2273 |
|
dbgPrintf("grubby installation device: %s\n", grubDevice); |
2274 |
|
|
2275 |
|
if (grubGetBootFromDeviceMap(grubDevice, bootPtr)) |
2276 |
|
dbgPrintf("error looking for grub boot device\n"); |
2277 |
|
else |
2278 |
|
dbgPrintf("grubby boot device: %s\n", *bootPtr); |
2279 |
|
|
2280 |
|
free(grubDevice); |
2281 |
|
return 0; |
2282 |
|
} |
2283 |
|
|
2284 |
|
int parseSuseGrubConf(int * lbaPtr, char ** bootPtr) { |
2285 |
|
/* |
2286 |
|
* This SuSE grub configuration file at this location is not your average |
2287 |
|
* grub configuration file, but instead the grub commands used to setup |
2288 |
|
* grub on that system. |
2289 |
|
*/ |
2290 |
|
const char * path; |
2291 |
|
const static char default_path[] = "/etc/grub.conf"; |
2292 |
|
|
2293 |
|
if ((path = getenv("GRUBBY_SUSE_GRUB_CONF")) == NULL) |
2294 |
|
path = default_path; |
2295 |
|
|
2296 |
|
if (!isSuseGrubConf(path)) return 1; |
2297 |
|
|
2298 |
|
if (lbaPtr) { |
2299 |
|
*lbaPtr = 0; |
2300 |
|
if (suseGrubConfGetLba(path, lbaPtr)) |
2301 |
|
return 1; |
2302 |
|
} |
2303 |
|
|
2304 |
|
if (bootPtr) { |
2305 |
|
*bootPtr = NULL; |
2306 |
|
suseGrubConfGetBoot(path, bootPtr); |
2307 |
|
} |
2308 |
|
|
2309 |
|
return 0; |
2310 |
} |
} |
2311 |
|
|
2312 |
int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) { |
int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) { |
2357 |
} |
} |
2358 |
|
|
2359 |
void dumpSysconfigGrub(void) { |
void dumpSysconfigGrub(void) { |
2360 |
char * boot; |
char * boot = NULL; |
2361 |
int lba; |
int lba; |
2362 |
|
|
2363 |
if (!parseSysconfigGrub(&lba, &boot)) { |
if (isSuseSystem()) { |
2364 |
if (lba) printf("lba\n"); |
if (parseSuseGrubConf(&lba, &boot)) { |
2365 |
if (boot) printf("boot=%s\n", boot); |
free(boot); |
2366 |
|
return; |
2367 |
|
} |
2368 |
|
} else { |
2369 |
|
if (parseSysconfigGrub(&lba, &boot)) { |
2370 |
|
free(boot); |
2371 |
|
return; |
2372 |
|
} |
2373 |
|
} |
2374 |
|
|
2375 |
|
if (lba) printf("lba\n"); |
2376 |
|
if (boot) { |
2377 |
|
printf("boot=%s\n", boot); |
2378 |
|
free(boot); |
2379 |
} |
} |
2380 |
} |
} |
2381 |
|
|
2574 |
free(line); |
free(line); |
2575 |
} |
} |
2576 |
|
|
|
static int isquote(char q) |
|
|
{ |
|
|
if (q == '\'' || q == '\"') |
|
|
return 1; |
|
|
return 0; |
|
|
} |
|
|
|
|
2577 |
static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi) |
static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi) |
2578 |
{ |
{ |
2579 |
struct singleLine newLine = { |
struct singleLine newLine = { |
3189 |
int fd; |
int fd; |
3190 |
unsigned char bootSect[512]; |
unsigned char bootSect[512]; |
3191 |
char * boot; |
char * boot; |
3192 |
|
int onSuse = isSuseSystem(); |
3193 |
|
|
3194 |
if (parseSysconfigGrub(NULL, &boot)) |
|
3195 |
return 0; |
if (onSuse) { |
3196 |
|
if (parseSuseGrubConf(NULL, &boot)) |
3197 |
|
return 0; |
3198 |
|
} else { |
3199 |
|
if (parseSysconfigGrub(NULL, &boot)) |
3200 |
|
return 0; |
3201 |
|
} |
3202 |
|
|
3203 |
/* assume grub is not installed -- not an error condition */ |
/* assume grub is not installed -- not an error condition */ |
3204 |
if (!boot) |
if (!boot) |
3217 |
} |
} |
3218 |
close(fd); |
close(fd); |
3219 |
|
|
3220 |
|
/* The more elaborate checks do not work on SuSE. The checks done |
3221 |
|
* seem to be reasonble (at least for now), so just return success |
3222 |
|
*/ |
3223 |
|
if (onSuse) |
3224 |
|
return 2; |
3225 |
|
|
3226 |
return checkDeviceBootloader(boot, bootSect); |
return checkDeviceBootloader(boot, bootSect); |
3227 |
} |
} |
3228 |
|
|
3256 |
return checkDeviceBootloader(boot, bootSect); |
return checkDeviceBootloader(boot, bootSect); |
3257 |
} |
} |
3258 |
|
|
3259 |
|
int checkForYaboot(struct grubConfig * config) { |
3260 |
|
/* |
3261 |
|
* This is a simplistic check that we consider good enough for own puporses |
3262 |
|
* |
3263 |
|
* If we were to properly check if yaboot is *installed* we'd need to: |
3264 |
|
* 1) get the system boot device (LT_BOOT) |
3265 |
|
* 2) considering it's a raw filesystem, check if the yaboot binary matches |
3266 |
|
* the content on the boot device |
3267 |
|
* 3) if not, copy the binary to a temporary file and run "addnote" on it |
3268 |
|
* 4) check again if binary and boot device contents match |
3269 |
|
*/ |
3270 |
|
if (!access("/etc/yaboot.conf", R_OK)) |
3271 |
|
return 2; |
3272 |
|
|
3273 |
|
return 1; |
3274 |
|
} |
3275 |
|
|
3276 |
|
int checkForElilo(struct grubConfig * config) { |
3277 |
|
if (!access("/etc/elilo.conf", R_OK)) |
3278 |
|
return 2; |
3279 |
|
|
3280 |
|
return 1; |
3281 |
|
} |
3282 |
|
|
3283 |
static char * getRootSpecifier(char * str) { |
static char * getRootSpecifier(char * str) { |
3284 |
char * idx, * rootspec = NULL; |
char * idx, * rootspec = NULL; |
3285 |
|
|
3294 |
static char * getInitrdVal(struct grubConfig * config, |
static char * getInitrdVal(struct grubConfig * config, |
3295 |
const char * prefix, struct singleLine *tmplLine, |
const char * prefix, struct singleLine *tmplLine, |
3296 |
const char * newKernelInitrd, |
const char * newKernelInitrd, |
3297 |
char ** extraInitrds, int extraInitrdCount) |
const char ** extraInitrds, int extraInitrdCount) |
3298 |
{ |
{ |
3299 |
char *initrdVal, *end; |
char *initrdVal, *end; |
3300 |
int i; |
int i; |
3339 |
|
|
3340 |
int addNewKernel(struct grubConfig * config, struct singleEntry * template, |
int addNewKernel(struct grubConfig * config, struct singleEntry * template, |
3341 |
const char * prefix, |
const char * prefix, |
3342 |
char * newKernelPath, char * newKernelTitle, |
const char * newKernelPath, const char * newKernelTitle, |
3343 |
char * newKernelArgs, char * newKernelInitrd, |
const char * newKernelArgs, const char * newKernelInitrd, |
3344 |
char ** extraInitrds, int extraInitrdCount, |
const char ** extraInitrds, int extraInitrdCount, |
3345 |
char * newMBKernel, char * newMBKernelArgs) { |
const char * newMBKernel, const char * newMBKernelArgs) { |
3346 |
struct singleEntry * new; |
struct singleEntry * new; |
3347 |
struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL; |
struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL; |
3348 |
int needs; |
int needs; |
3750 |
int displayDefault = 0; |
int displayDefault = 0; |
3751 |
int displayDefaultIndex = 0; |
int displayDefaultIndex = 0; |
3752 |
int displayDefaultTitle = 0; |
int displayDefaultTitle = 0; |
3753 |
|
int defaultIndex = -1; |
3754 |
struct poptOption options[] = { |
struct poptOption options[] = { |
3755 |
{ "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0, |
{ "add-kernel", 0, POPT_ARG_STRING, &newKernelPath, 0, |
3756 |
_("add an entry for the specified kernel"), _("kernel-path") }, |
_("add an entry for the specified kernel"), _("kernel-path") }, |
3768 |
{ "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0, |
{ "boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0, |
3769 |
_("filestystem which contains /boot directory (for testing only)"), |
_("filestystem which contains /boot directory (for testing only)"), |
3770 |
_("bootfs") }, |
_("bootfs") }, |
3771 |
#if defined(__i386__) || defined(__x86_64__) |
#if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__) |
3772 |
{ "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0, |
{ "bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0, |
3773 |
_("check if lilo is installed on lilo.conf boot sector") }, |
_("check which bootloader is installed on boot sector") }, |
3774 |
#endif |
#endif |
3775 |
{ "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0, |
{ "config-file", 'c', POPT_ARG_STRING, &grubConfig, 0, |
3776 |
_("path to grub config file to update (\"-\" for stdin)"), |
_("path to grub config file to update (\"-\" for stdin)"), |
3823 |
{ "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0, |
{ "set-default", 0, POPT_ARG_STRING, &defaultKernel, 0, |
3824 |
_("make the first entry referencing the specified kernel " |
_("make the first entry referencing the specified kernel " |
3825 |
"the default"), _("kernel-path") }, |
"the default"), _("kernel-path") }, |
3826 |
|
{ "set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0, |
3827 |
|
_("make the given entry index the default entry"), |
3828 |
|
_("entry-index") }, |
3829 |
{ "silo", 0, POPT_ARG_NONE, &configureSilo, 0, |
{ "silo", 0, POPT_ARG_NONE, &configureSilo, 0, |
3830 |
_("configure silo bootloader") }, |
_("configure silo bootloader") }, |
3831 |
{ "title", 0, POPT_ARG_STRING, &newKernelTitle, 0, |
{ "title", 0, POPT_ARG_STRING, &newKernelTitle, 0, |
3908 |
} |
} |
3909 |
|
|
3910 |
if (!cfi) { |
if (!cfi) { |
3911 |
|
if (grub2FindConfig(&grub2ConfigType)) |
3912 |
|
cfi = &grub2ConfigType; |
3913 |
|
else |
3914 |
#ifdef __ia64__ |
#ifdef __ia64__ |
3915 |
cfi = &eliloConfigType; |
cfi = &eliloConfigType; |
3916 |
#elif __powerpc__ |
#elif __powerpc__ |
3917 |
cfi = &yabootConfigType; |
cfi = &yabootConfigType; |
3918 |
#elif __sparc__ |
#elif __sparc__ |
3919 |
cfi = &siloConfigType; |
cfi = &siloConfigType; |
3920 |
#elif __s390__ |
#elif __s390__ |
3921 |
cfi = &ziplConfigType; |
cfi = &ziplConfigType; |
3922 |
#elif __s390x__ |
#elif __s390x__ |
3923 |
cfi = &ziplConfigtype; |
cfi = &ziplConfigtype; |
3924 |
#else |
#else |
|
if (grub2FindConfig(&grub2ConfigType)) |
|
|
cfi = &grub2ConfigType; |
|
|
else |
|
3925 |
cfi = &grubConfigType; |
cfi = &grubConfigType; |
3926 |
#endif |
#endif |
3927 |
} |
} |
3934 |
} |
} |
3935 |
|
|
3936 |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion || |
3937 |
newKernelPath || removeKernelPath || makeDefault || |
newKernelPath || removeKernelPath || makeDefault || |
3938 |
defaultKernel || displayDefaultIndex || displayDefaultTitle)) { |
defaultKernel || displayDefaultIndex || displayDefaultTitle || |
3939 |
|
(defaultIndex >= 0))) { |
3940 |
fprintf(stderr, _("grubby: --bootloader-probe may not be used with " |
fprintf(stderr, _("grubby: --bootloader-probe may not be used with " |
3941 |
"specified option")); |
"specified option")); |
3942 |
return 1; |
return 1; |
3978 |
makeDefault = 1; |
makeDefault = 1; |
3979 |
defaultKernel = NULL; |
defaultKernel = NULL; |
3980 |
} |
} |
3981 |
|
else if (defaultKernel && (defaultIndex >= 0)) { |
3982 |
|
fprintf(stderr, _("grubby: --set-default and --set-default-index " |
3983 |
|
"may not be used together\n")); |
3984 |
|
return 1; |
3985 |
|
} |
3986 |
|
|
3987 |
if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) { |
if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) { |
3988 |
fprintf(stderr, _("grubby: output file must be specified if stdin " |
fprintf(stderr, _("grubby: output file must be specified if stdin " |
3991 |
} |
} |
3992 |
|
|
3993 |
if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel |
if (!removeKernelPath && !newKernelPath && !displayDefault && !defaultKernel |
3994 |
&& !kernelInfo && !bootloaderProbe && !updateKernelPath |
&& !kernelInfo && !bootloaderProbe && !updateKernelPath |
3995 |
&& !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle) { |
&& !removeMBKernel && !displayDefaultIndex && !displayDefaultTitle |
3996 |
|
&& (defaultIndex == -1)) { |
3997 |
fprintf(stderr, _("grubby: no action specified\n")); |
fprintf(stderr, _("grubby: no action specified\n")); |
3998 |
return 1; |
return 1; |
3999 |
} |
} |
4020 |
} |
} |
4021 |
|
|
4022 |
if (bootloaderProbe) { |
if (bootloaderProbe) { |
4023 |
int lrc = 0, grc = 0, gr2c = 0, erc = 0; |
int lrc = 0, grc = 0, gr2c = 0, extrc = 0, yrc = 0, erc = 0; |
4024 |
struct grubConfig * lconfig, * gconfig; |
struct grubConfig * lconfig, * gconfig, * yconfig, * econfig; |
4025 |
|
|
4026 |
const char *grub2config = grub2FindConfig(&grub2ConfigType); |
const char *grub2config = grub2FindConfig(&grub2ConfigType); |
4027 |
if (grub2config) { |
if (grub2config) { |
4049 |
lrc = checkForLilo(lconfig); |
lrc = checkForLilo(lconfig); |
4050 |
} |
} |
4051 |
|
|
4052 |
|
if (!access(eliloConfigType.defaultConfig, F_OK)) { |
4053 |
|
econfig = readConfig(eliloConfigType.defaultConfig, |
4054 |
|
&eliloConfigType); |
4055 |
|
if (!econfig) |
4056 |
|
erc = 1; |
4057 |
|
else |
4058 |
|
erc = checkForElilo(econfig); |
4059 |
|
} |
4060 |
|
|
4061 |
if (!access(extlinuxConfigType.defaultConfig, F_OK)) { |
if (!access(extlinuxConfigType.defaultConfig, F_OK)) { |
4062 |
lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType); |
lconfig = readConfig(extlinuxConfigType.defaultConfig, &extlinuxConfigType); |
4063 |
if (!lconfig) |
if (!lconfig) |
4064 |
erc = 1; |
extrc = 1; |
4065 |
else |
else |
4066 |
erc = checkForExtLinux(lconfig); |
extrc = checkForExtLinux(lconfig); |
4067 |
} |
} |
4068 |
|
|
4069 |
if (lrc == 1 || grc == 1 || gr2c == 1) return 1; |
|
4070 |
|
if (!access(yabootConfigType.defaultConfig, F_OK)) { |
4071 |
|
yconfig = readConfig(yabootConfigType.defaultConfig, |
4072 |
|
&yabootConfigType); |
4073 |
|
if (!yconfig) |
4074 |
|
yrc = 1; |
4075 |
|
else |
4076 |
|
yrc = checkForYaboot(yconfig); |
4077 |
|
} |
4078 |
|
|
4079 |
|
if (lrc == 1 || grc == 1 || gr2c == 1 || extrc == 1 || yrc == 1 || |
4080 |
|
erc == 1) |
4081 |
|
return 1; |
4082 |
|
|
4083 |
if (lrc == 2) printf("lilo\n"); |
if (lrc == 2) printf("lilo\n"); |
4084 |
if (gr2c == 2) printf("grub2\n"); |
if (gr2c == 2) printf("grub2\n"); |
4085 |
if (grc == 2) printf("grub\n"); |
if (grc == 2) printf("grub\n"); |
4086 |
if (erc == 2) printf("extlinux\n"); |
if (extrc == 2) printf("extlinux\n"); |
4087 |
|
if (yrc == 2) printf("yaboot\n"); |
4088 |
|
if (erc == 2) printf("elilo\n"); |
4089 |
|
|
4090 |
return 0; |
return 0; |
4091 |
} |
} |
4126 |
printf("%s\n", line->elements[1].item); |
printf("%s\n", line->elements[1].item); |
4127 |
|
|
4128 |
} else { |
} else { |
4129 |
int i; |
char * title; |
|
size_t len; |
|
|
char * start; |
|
|
char * tmp; |
|
4130 |
|
|
4131 |
dbgPrintf("This is GRUB2, default title is embeded in menuentry\n"); |
dbgPrintf("This is GRUB2, default title is embeded in menuentry\n"); |
4132 |
line = getLineByType(LT_MENUENTRY, entry->lines); |
line = getLineByType(LT_MENUENTRY, entry->lines); |
4133 |
if (!line) return 0; |
if (!line) return 0; |
4134 |
|
title = grub2ExtractTitle(line); |
4135 |
for (i = 0; i < line->numElements; i++) { |
if (title) |
4136 |
|
printf("%s\n", title); |
|
if (!strcmp(line->elements[i].item, "menuentry")) |
|
|
continue; |
|
|
|
|
|
if (*line->elements[i].item == '\'') |
|
|
start = line->elements[i].item + 1; |
|
|
else |
|
|
start = line->elements[i].item; |
|
|
|
|
|
len = strlen(start); |
|
|
if (*(start + len - 1) == '\'') { |
|
|
tmp = strdup(start); |
|
|
*(tmp + len - 1) = '\0'; |
|
|
printf("%s", tmp); |
|
|
free(tmp); |
|
|
break; |
|
|
} else { |
|
|
printf("%s ", start); |
|
|
} |
|
|
} |
|
|
printf("\n"); |
|
4137 |
} |
} |
4138 |
return 0; |
return 0; |
4139 |
|
|
4152 |
markRemovedImage(config, removeKernelPath, bootPrefix); |
markRemovedImage(config, removeKernelPath, bootPrefix); |
4153 |
markRemovedImage(config, removeMBKernel, bootPrefix); |
markRemovedImage(config, removeMBKernel, bootPrefix); |
4154 |
setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault, |
setDefaultImage(config, newKernelPath != NULL, defaultKernel, makeDefault, |
4155 |
bootPrefix, flags); |
bootPrefix, flags, defaultIndex); |
4156 |
setFallbackImage(config, newKernelPath != NULL); |
setFallbackImage(config, newKernelPath != NULL); |
4157 |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
if (updateImage(config, updateKernelPath, bootPrefix, newKernelArgs, |
4158 |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1; |
removeArgs, newMBKernelArgs, removeMBKernelArgs)) return 1; |
4162 |
} |
} |
4163 |
if (addNewKernel(config, template, bootPrefix, newKernelPath, |
if (addNewKernel(config, template, bootPrefix, newKernelPath, |
4164 |
newKernelTitle, newKernelArgs, newKernelInitrd, |
newKernelTitle, newKernelArgs, newKernelInitrd, |
4165 |
extraInitrds, extraInitrdCount, |
(const char **)extraInitrds, extraInitrdCount, |
4166 |
newMBKernel, newMBKernelArgs)) return 1; |
newMBKernel, newMBKernelArgs)) return 1; |
4167 |
|
|
4168 |
|
|