]> git.proxmox.com Git - grub2.git/blob - grub-core/kern/ieee1275/mmap.c
Import grub2_2.02+dfsg1.orig.tar.xz
[grub2.git] / grub-core / kern / ieee1275 / mmap.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2003,2004,2005,2007,2008 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/memory.h>
20 #include <grub/ieee1275/ieee1275.h>
21 #include <grub/types.h>
22
23 grub_err_t
24 grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
25 {
26 grub_ieee1275_phandle_t root;
27 grub_ieee1275_phandle_t memory;
28 grub_uint32_t available[128];
29 grub_ssize_t available_size;
30 grub_uint32_t address_cells = 1;
31 grub_uint32_t size_cells = 1;
32 int i;
33
34 /* Determine the format of each entry in `available'. */
35 grub_ieee1275_finddevice ("/", &root);
36 grub_ieee1275_get_integer_property (root, "#address-cells", &address_cells,
37 sizeof address_cells, 0);
38 grub_ieee1275_get_integer_property (root, "#size-cells", &size_cells,
39 sizeof size_cells, 0);
40
41 if (size_cells > address_cells)
42 address_cells = size_cells;
43
44 /* Load `/memory/available'. */
45 if (grub_ieee1275_finddevice ("/memory", &memory))
46 return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
47 "couldn't find /memory node");
48 if (grub_ieee1275_get_integer_property (memory, "available", available,
49 sizeof available, &available_size))
50 return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
51 "couldn't examine /memory/available property");
52 if (available_size < 0 || (grub_size_t) available_size > sizeof (available))
53 return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
54 "/memory response buffer exceeded");
55
56 if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_BROKEN_ADDRESS_CELLS))
57 {
58 address_cells = 1;
59 size_cells = 1;
60 }
61
62 /* Decode each entry and call `hook'. */
63 i = 0;
64 available_size /= sizeof (grub_uint32_t);
65 while (i < available_size)
66 {
67 grub_uint64_t address;
68 grub_uint64_t size;
69
70 address = available[i++];
71 if (address_cells == 2)
72 address = (address << 32) | available[i++];
73
74 size = available[i++];
75 if (size_cells == 2)
76 size = (size << 32) | available[i++];
77
78 if (hook (address, size, GRUB_MEMORY_AVAILABLE, hook_data))
79 break;
80 }
81
82 return grub_errno;
83 }