Harmonize ieee1275's grub_available_iterate() with the generic
[grub2/phcoder.git] / kern / ieee1275 / mmap.c
blob5b30dbb9ea0a3f2db0124a4c9c427ca32f19af61
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 /* Load `/memory/available'. */
42 if (grub_ieee1275_finddevice ("/memory", &memory))
43 return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
44 "Couldn't find /memory node");
45 if (grub_ieee1275_get_integer_property (memory, "available", available,
46 sizeof available, &available_size))
47 return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
48 "Couldn't examine /memory/available property");
50 /* Decode each entry and call `hook'. */
51 i = 0;
52 available_size /= sizeof (grub_uint32_t);
53 while (i < available_size)
55 grub_uint64_t address;
56 grub_uint64_t size;
58 address = available[i++];
59 if (address_cells == 2)
60 address = (address << 32) | available[i++];
62 size = available[i++];
63 if (size_cells == 2)
64 size = (size << 32) | available[i++];
66 if (hook (address, size, GRUB_MACHINE_MEMORY_AVAILABLE))
67 break;
70 return grub_errno;