| 1 |
/* |
| 2 |
* GRUB -- GRand Unified Bootloader |
| 3 |
* Copyright (C) 2009,2010 Free Software Foundation, Inc. |
| 4 |
* |
| 5 |
* GRUB is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* GRUB is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with GRUB. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <grub/kernel.h> |
| 20 |
#include <grub/misc.h> |
| 21 |
#include <grub/env.h> |
| 22 |
#include <grub/time.h> |
| 23 |
#include <grub/types.h> |
| 24 |
#include <grub/misc.h> |
| 25 |
#include <grub/mm.h> |
| 26 |
#include <grub/time.h> |
| 27 |
#include <grub/machine/kernel.h> |
| 28 |
#include <grub/machine/memory.h> |
| 29 |
#include <grub/arc/console.h> |
| 30 |
#include <grub/cpu/memory.h> |
| 31 |
#include <grub/cpu/time.h> |
| 32 |
#include <grub/memory.h> |
| 33 |
#include <grub/term.h> |
| 34 |
#include <grub/arc/arc.h> |
| 35 |
#include <grub/offsets.h> |
| 36 |
#include <grub/i18n.h> |
| 37 |
#include <grub/disk.h> |
| 38 |
#include <grub/partition.h> |
| 39 |
|
| 40 |
const char *type_names[] = { |
| 41 |
#ifdef GRUB_CPU_WORDS_BIGENDIAN |
| 42 |
NULL, |
| 43 |
#endif |
| 44 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 45 |
"eisa", "tc", "scsi", "dti", "multi", "disk", "tape", "cdrom", "worm", |
| 46 |
"serial", "net", "video", "par", "point", "key", "audio", "other", |
| 47 |
"rdisk", "fdisk", "tape", "modem", "monitor", "print", "pointer", |
| 48 |
"keyboard", "term", |
| 49 |
#ifndef GRUB_CPU_WORDS_BIGENDIAN |
| 50 |
"other", |
| 51 |
#endif |
| 52 |
"line", "network", NULL |
| 53 |
}; |
| 54 |
|
| 55 |
static int |
| 56 |
iterate_rec (const char *prefix, const struct grub_arc_component *parent, |
| 57 |
grub_arc_iterate_devs_hook_t hook, void *hook_data, |
| 58 |
int alt_names) |
| 59 |
{ |
| 60 |
const struct grub_arc_component *comp; |
| 61 |
FOR_ARC_CHILDREN(comp, parent) |
| 62 |
{ |
| 63 |
char *name; |
| 64 |
const char *cname = NULL; |
| 65 |
if (comp->type < ARRAY_SIZE (type_names)) |
| 66 |
cname = type_names[comp->type]; |
| 67 |
if (!cname) |
| 68 |
cname = "unknown"; |
| 69 |
if (alt_names) |
| 70 |
name = grub_xasprintf ("%s/%s%lu", prefix, cname, comp->key); |
| 71 |
else |
| 72 |
name = grub_xasprintf ("%s%s(%lu)", prefix, cname, comp->key); |
| 73 |
if (!name) |
| 74 |
return 1; |
| 75 |
if (hook (name, comp, hook_data)) |
| 76 |
{ |
| 77 |
grub_free (name); |
| 78 |
return 1; |
| 79 |
} |
| 80 |
if (iterate_rec ((parent ? name : prefix), comp, hook, hook_data, |
| 81 |
alt_names)) |
| 82 |
{ |
| 83 |
grub_free (name); |
| 84 |
return 1; |
| 85 |
} |
| 86 |
grub_free (name); |
| 87 |
} |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
int |
| 92 |
grub_arc_iterate_devs (grub_arc_iterate_devs_hook_t hook, void *hook_data, |
| 93 |
int alt_names) |
| 94 |
{ |
| 95 |
return iterate_rec ((alt_names ? "arc" : ""), NULL, hook, hook_data, |
| 96 |
alt_names); |
| 97 |
} |
| 98 |
|
| 99 |
grub_err_t |
| 100 |
grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data) |
| 101 |
{ |
| 102 |
struct grub_arc_memory_descriptor *cur = NULL; |
| 103 |
while (1) |
| 104 |
{ |
| 105 |
grub_memory_type_t type; |
| 106 |
cur = GRUB_ARC_FIRMWARE_VECTOR->getmemorydescriptor (cur); |
| 107 |
if (!cur) |
| 108 |
return GRUB_ERR_NONE; |
| 109 |
switch (cur->type) |
| 110 |
{ |
| 111 |
case GRUB_ARC_MEMORY_EXCEPTION_BLOCK: |
| 112 |
case GRUB_ARC_MEMORY_SYSTEM_PARAMETER_BLOCK: |
| 113 |
case GRUB_ARC_MEMORY_FW_PERMANENT: |
| 114 |
default: |
| 115 |
type = GRUB_MEMORY_RESERVED; |
| 116 |
break; |
| 117 |
|
| 118 |
case GRUB_ARC_MEMORY_FW_TEMPORARY: |
| 119 |
case GRUB_ARC_MEMORY_FREE: |
| 120 |
case GRUB_ARC_MEMORY_LOADED: |
| 121 |
case GRUB_ARC_MEMORY_FREE_CONTIGUOUS: |
| 122 |
type = GRUB_MEMORY_AVAILABLE; |
| 123 |
break; |
| 124 |
case GRUB_ARC_MEMORY_BADRAM: |
| 125 |
type = GRUB_MEMORY_BADRAM; |
| 126 |
break; |
| 127 |
} |
| 128 |
if (hook (((grub_uint64_t) cur->start_page) << 12, |
| 129 |
((grub_uint64_t) cur->num_pages) << 12, type, hook_data)) |
| 130 |
return GRUB_ERR_NONE; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
char * |
| 135 |
grub_arc_alt_name_to_norm (const char *name, const char *suffix) |
| 136 |
{ |
| 137 |
char *optr; |
| 138 |
const char *iptr; |
| 139 |
char * ret = grub_malloc (2 * grub_strlen (name) + grub_strlen (suffix)); |
| 140 |
int state = 0; |
| 141 |
|
| 142 |
if (!ret) |
| 143 |
return NULL; |
| 144 |
optr = ret; |
| 145 |
for (iptr = name + 4; *iptr; iptr++) |
| 146 |
if (state == 0) |
| 147 |
{ |
| 148 |
if (!grub_isdigit (*iptr)) |
| 149 |
*optr++ = *iptr; |
| 150 |
else |
| 151 |
{ |
| 152 |
*optr++ = '('; |
| 153 |
*optr++ = *iptr; |
| 154 |
state = 1; |
| 155 |
} |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
if (grub_isdigit (*iptr)) |
| 160 |
*optr++ = *iptr; |
| 161 |
else |
| 162 |
{ |
| 163 |
*optr++ = ')'; |
| 164 |
state = 0; |
| 165 |
} |
| 166 |
} |
| 167 |
if (state) |
| 168 |
*optr++ = ')'; |
| 169 |
grub_strcpy (optr, suffix); |
| 170 |
return ret; |
| 171 |
} |
| 172 |
|
| 173 |
static char * |
| 174 |
norm_name_to_alt (const char *name) |
| 175 |
{ |
| 176 |
char *optr; |
| 177 |
const char *iptr; |
| 178 |
int state = 0; |
| 179 |
char * ret = grub_malloc (grub_strlen (name) + sizeof ("arc/")); |
| 180 |
|
| 181 |
if (!ret) |
| 182 |
return NULL; |
| 183 |
optr = grub_stpcpy (ret, "arc/"); |
| 184 |
for (iptr = name; *iptr; iptr++) |
| 185 |
{ |
| 186 |
if (state == 3) |
| 187 |
{ |
| 188 |
*optr++ = '/'; |
| 189 |
state = 0; |
| 190 |
} |
| 191 |
if (*iptr == '(') |
| 192 |
{ |
| 193 |
state = 1; |
| 194 |
continue; |
| 195 |
} |
| 196 |
if (*iptr == ')') |
| 197 |
{ |
| 198 |
if (state == 1) |
| 199 |
*optr++ = '0'; |
| 200 |
state = 3; |
| 201 |
continue; |
| 202 |
} |
| 203 |
*optr++ = *iptr; |
| 204 |
if (state == 1) |
| 205 |
state = 2; |
| 206 |
} |
| 207 |
*optr = '\0'; |
| 208 |
return ret; |
| 209 |
} |
| 210 |
|
| 211 |
extern grub_uint32_t grub_total_modules_size __attribute__ ((section(".text"))); |
| 212 |
grub_addr_t grub_modbase; |
| 213 |
|
| 214 |
extern char _end[]; |
| 215 |
static char boot_location[256]; |
| 216 |
|
| 217 |
void |
| 218 |
grub_machine_init (void) |
| 219 |
{ |
| 220 |
struct grub_arc_memory_descriptor *cur = NULL; |
| 221 |
grub_addr_t modend; |
| 222 |
|
| 223 |
grub_memcpy (boot_location, |
| 224 |
(char *) (GRUB_DECOMPRESSOR_LINK_ADDR - 256), 256); |
| 225 |
|
| 226 |
grub_modbase = ALIGN_UP ((grub_addr_t) _end, GRUB_KERNEL_MACHINE_MOD_ALIGN); |
| 227 |
modend = grub_modbase + grub_total_modules_size; |
| 228 |
grub_console_init_early (); |
| 229 |
|
| 230 |
/* FIXME: measure this. */ |
| 231 |
grub_arch_cpuclock = 150000000; |
| 232 |
grub_install_get_time_ms (grub_rtc_get_time_ms); |
| 233 |
|
| 234 |
while (1) |
| 235 |
{ |
| 236 |
grub_uint64_t start, end; |
| 237 |
cur = GRUB_ARC_FIRMWARE_VECTOR->getmemorydescriptor (cur); |
| 238 |
if (!cur) |
| 239 |
break; |
| 240 |
if (cur->type != GRUB_ARC_MEMORY_FREE |
| 241 |
&& cur->type != GRUB_ARC_MEMORY_LOADED |
| 242 |
&& cur->type != GRUB_ARC_MEMORY_FREE_CONTIGUOUS) |
| 243 |
continue; |
| 244 |
start = ((grub_uint64_t) cur->start_page) << 12; |
| 245 |
end = ((grub_uint64_t) cur->num_pages) << 12; |
| 246 |
end += start; |
| 247 |
if ((grub_uint64_t) start < (modend & 0x1fffffff)) |
| 248 |
start = (modend & 0x1fffffff); |
| 249 |
if ((grub_uint64_t) end > 0x20000000) |
| 250 |
end = 0x20000000; |
| 251 |
if (end > start) |
| 252 |
grub_mm_init_region ((void *) (grub_addr_t) (start | 0x80000000), |
| 253 |
end - start); |
| 254 |
} |
| 255 |
|
| 256 |
grub_console_init_lately (); |
| 257 |
|
| 258 |
grub_arcdisk_init (); |
| 259 |
} |
| 260 |
|
| 261 |
void |
| 262 |
grub_machine_fini (int flags __attribute__ ((unused))) |
| 263 |
{ |
| 264 |
} |
| 265 |
|
| 266 |
void |
| 267 |
grub_halt (void) |
| 268 |
{ |
| 269 |
GRUB_ARC_FIRMWARE_VECTOR->powerdown (); |
| 270 |
|
| 271 |
grub_millisleep (1500); |
| 272 |
|
| 273 |
grub_puts_ (N_("Shutdown failed")); |
| 274 |
grub_refresh (); |
| 275 |
while (1); |
| 276 |
} |
| 277 |
|
| 278 |
void |
| 279 |
grub_exit (void) |
| 280 |
{ |
| 281 |
GRUB_ARC_FIRMWARE_VECTOR->exit (); |
| 282 |
|
| 283 |
grub_millisleep (1500); |
| 284 |
|
| 285 |
grub_puts_ (N_("Exit failed")); |
| 286 |
grub_refresh (); |
| 287 |
while (1); |
| 288 |
} |
| 289 |
|
| 290 |
static char * |
| 291 |
get_part (char *dev) |
| 292 |
{ |
| 293 |
char *ptr; |
| 294 |
if (!*dev) |
| 295 |
return 0; |
| 296 |
ptr = dev + grub_strlen (dev) - 1; |
| 297 |
if (ptr == dev || *ptr != ')') |
| 298 |
return 0; |
| 299 |
ptr--; |
| 300 |
while (grub_isdigit (*ptr) && ptr > dev) |
| 301 |
ptr--; |
| 302 |
if (*ptr != '(' || ptr == dev) |
| 303 |
return 0; |
| 304 |
ptr--; |
| 305 |
if (ptr - dev < (int) sizeof ("partition") - 2) |
| 306 |
return 0; |
| 307 |
ptr -= sizeof ("partition") - 2; |
| 308 |
if (grub_memcmp (ptr, "partition", sizeof ("partition") - 1) != 0) |
| 309 |
return 0; |
| 310 |
return ptr; |
| 311 |
} |
| 312 |
|
| 313 |
static grub_disk_addr_t |
| 314 |
get_partition_offset (char *part, grub_disk_addr_t *en) |
| 315 |
{ |
| 316 |
grub_arc_fileno_t handle; |
| 317 |
grub_disk_addr_t ret = -1; |
| 318 |
struct grub_arc_fileinfo info; |
| 319 |
grub_arc_err_t r; |
| 320 |
|
| 321 |
if (GRUB_ARC_FIRMWARE_VECTOR->open (part, GRUB_ARC_FILE_ACCESS_OPEN_RO, |
| 322 |
&handle)) |
| 323 |
return -1; |
| 324 |
|
| 325 |
r = GRUB_ARC_FIRMWARE_VECTOR->getfileinformation (handle, &info); |
| 326 |
if (!r) |
| 327 |
{ |
| 328 |
ret = (info.start >> 9); |
| 329 |
*en = (info.end >> 9); |
| 330 |
} |
| 331 |
GRUB_ARC_FIRMWARE_VECTOR->close (handle); |
| 332 |
return ret; |
| 333 |
} |
| 334 |
|
| 335 |
struct get_device_name_ctx |
| 336 |
{ |
| 337 |
char *partition_name; |
| 338 |
grub_disk_addr_t poff, pend; |
| 339 |
}; |
| 340 |
|
| 341 |
static int |
| 342 |
get_device_name_iter (grub_disk_t disk __attribute__ ((unused)), |
| 343 |
const grub_partition_t part, void *data) |
| 344 |
{ |
| 345 |
struct get_device_name_ctx *ctx = data; |
| 346 |
|
| 347 |
if (grub_partition_get_start (part) == ctx->poff |
| 348 |
&& grub_partition_get_len (part) == ctx->pend) |
| 349 |
{ |
| 350 |
ctx->partition_name = grub_partition_get_name (part); |
| 351 |
return 1; |
| 352 |
} |
| 353 |
|
| 354 |
return 0; |
| 355 |
} |
| 356 |
|
| 357 |
void |
| 358 |
grub_machine_get_bootlocation (char **device, char **path) |
| 359 |
{ |
| 360 |
char *loaddev = boot_location; |
| 361 |
char *pptr, *partptr; |
| 362 |
char *dname; |
| 363 |
grub_disk_addr_t poff = -1, pend = -1; |
| 364 |
struct get_device_name_ctx ctx; |
| 365 |
grub_disk_t parent = 0; |
| 366 |
unsigned i; |
| 367 |
|
| 368 |
for (i = 0; i < ARRAY_SIZE (type_names); i++) |
| 369 |
if (type_names[i] |
| 370 |
&& grub_memcmp (loaddev, type_names[i], grub_strlen (type_names[i])) == 0 |
| 371 |
&& loaddev[grub_strlen (type_names[i])] == '(') |
| 372 |
break; |
| 373 |
if (i == ARRAY_SIZE (type_names)) |
| 374 |
pptr = loaddev; |
| 375 |
else |
| 376 |
for (pptr = loaddev; *pptr && *pptr != '/' && *pptr != '\\'; pptr++); |
| 377 |
if (*pptr) |
| 378 |
{ |
| 379 |
char *iptr, *optr; |
| 380 |
char sep = *pptr; |
| 381 |
*path = grub_malloc (grub_strlen (pptr) + 1); |
| 382 |
if (!*path) |
| 383 |
return; |
| 384 |
for (iptr = pptr, optr = *path; *iptr; iptr++, optr++) |
| 385 |
if (*iptr == sep) |
| 386 |
*optr = '/'; |
| 387 |
else |
| 388 |
*optr = *iptr; |
| 389 |
*optr = '\0'; |
| 390 |
*path = grub_strdup (pptr); |
| 391 |
*pptr = '\0'; |
| 392 |
} |
| 393 |
|
| 394 |
if (*loaddev == '\0') |
| 395 |
{ |
| 396 |
const char *syspart = 0; |
| 397 |
|
| 398 |
if (GRUB_ARC_SYSTEM_PARAMETER_BLOCK->firmware_vector_length |
| 399 |
>= (unsigned) ((char *) (&GRUB_ARC_FIRMWARE_VECTOR->getenvironmentvariable + 1) |
| 400 |
- (char *) GRUB_ARC_FIRMWARE_VECTOR) |
| 401 |
&& GRUB_ARC_FIRMWARE_VECTOR->getenvironmentvariable) |
| 402 |
syspart = GRUB_ARC_FIRMWARE_VECTOR->getenvironmentvariable ("SystemPartition"); |
| 403 |
if (!syspart) |
| 404 |
return; |
| 405 |
loaddev = grub_strdup (syspart); |
| 406 |
if (loaddev == NULL) |
| 407 |
return; |
| 408 |
|
| 409 |
} |
| 410 |
|
| 411 |
partptr = get_part (loaddev); |
| 412 |
if (partptr) |
| 413 |
{ |
| 414 |
poff = get_partition_offset (loaddev, &pend); |
| 415 |
*partptr = '\0'; |
| 416 |
} |
| 417 |
dname = norm_name_to_alt (loaddev); |
| 418 |
if (poff == (grub_addr_t) -1) |
| 419 |
{ |
| 420 |
*device = dname; |
| 421 |
if (loaddev != boot_location) |
| 422 |
grub_free (loaddev); |
| 423 |
return; |
| 424 |
} |
| 425 |
|
| 426 |
parent = grub_disk_open (dname); |
| 427 |
if (!parent) |
| 428 |
{ |
| 429 |
*device = dname; |
| 430 |
if (loaddev != boot_location) |
| 431 |
grub_free (loaddev); |
| 432 |
return; |
| 433 |
} |
| 434 |
|
| 435 |
if (poff == 0 |
| 436 |
&& pend == grub_disk_native_sectors (parent)) |
| 437 |
{ |
| 438 |
grub_disk_close (parent); |
| 439 |
*device = dname; |
| 440 |
if (loaddev != boot_location) |
| 441 |
grub_free (loaddev); |
| 442 |
return; |
| 443 |
} |
| 444 |
|
| 445 |
ctx.partition_name = NULL; |
| 446 |
ctx.poff = poff; |
| 447 |
ctx.pend = pend; |
| 448 |
|
| 449 |
grub_partition_iterate (parent, get_device_name_iter, &ctx); |
| 450 |
grub_disk_close (parent); |
| 451 |
|
| 452 |
if (! ctx.partition_name) |
| 453 |
{ |
| 454 |
*device = dname; |
| 455 |
if (loaddev != boot_location) |
| 456 |
grub_free (loaddev); |
| 457 |
return; |
| 458 |
} |
| 459 |
|
| 460 |
*device = grub_xasprintf ("%s,%s", dname, |
| 461 |
ctx.partition_name); |
| 462 |
grub_free (ctx.partition_name); |
| 463 |
grub_free (dname); |
| 464 |
if (loaddev != boot_location) |
| 465 |
grub_free (loaddev); |
| 466 |
} |