2009-07-05 Pavel Roskin <proski@gnu.org>
[grub2/bean.git] / kern / ieee1275 / mmap.c
blob317a1211762ea281bfe85b54b6ed131b0c801648
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2003,2004,2005,2007,2008 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <grub/machine/memory.h>
20 #include <grub/ieee1275/ieee1275.h>
21 #include <grub/types.h>
23 grub_err_t
24 grub_machine_mmap_iterate (int NESTED_FUNC_ATTR (*hook) (grub_uint64_t, grub_uint64_t, grub_uint32_t))
26 grub_ieee1275_phandle_t root;
27 grub_ieee1275_phandle_t memory;
28 grub_uint32_t available[32];
29 grub_ssize_t available_size;
30 grub_uint32_t address_cells = 1;
31 grub_uint32_t size_cells = 1;
32 int i;
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);
41 if (size_cells > address_cells)
42 address_cells = size_cells;
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");
53 /* Decode each entry and call `hook'. */
54 i = 0;
55 available_size /= sizeof (grub_uint32_t);
56 while (i < available_size)
58 grub_uint64_t address;
59 grub_uint64_t size;
61 address = available[i++];
62 if (address_cells == 2)
63 address = (address << 32) | available[i++];
65 size = available[i++];
66 if (size_cells == 2)
67 size = (size << 32) | available[i++];
69 if (hook (address, size, GRUB_MACHINE_MEMORY_AVAILABLE))
70 break;
73 return grub_errno;