summaryrefslogtreecommitdiffstats
path: root/grub-core/kern/uboot/hw.c
blob: 272b83bd76b218dc2c0488f76caccf1207f7b998 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* hw.c - U-Boot hardware discovery */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2013  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/kernel.h>
#include <grub/memory.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/offsets.h>
#include <grub/machine/kernel.h>
#include <grub/uboot/disk.h>
#include <grub/uboot/uboot.h>
#include <grub/uboot/api_public.h>

grub_addr_t start_of_ram;

/*
 * grub_uboot_probe_memory():
 *   Queries U-Boot for available memory regions.
 *
 *   Sets up heap near the image in memory and sets up "start_of_ram".
 */
void
grub_uboot_mm_init (void)
{
  struct sys_info *si = grub_uboot_get_sys_info ();

  grub_mm_init_region ((void *) grub_modules_get_end (),
		       GRUB_KERNEL_MACHINE_HEAP_SIZE);

  if (si && (si->mr_no != 0))
    {
      int i;
      start_of_ram = GRUB_UINT_MAX;

      for (i = 0; i < si->mr_no; i++)
	if ((si->mr[i].flags & MR_ATTR_MASK) == MR_ATTR_DRAM)
	  if (si->mr[i].start < start_of_ram)
	    start_of_ram = si->mr[i].start;
    }
}

/*
 * grub_uboot_probe_hardware():
 */
grub_err_t
grub_uboot_probe_hardware (void)
{
  int devcount, i;

  devcount = grub_uboot_dev_enum ();
  grub_dprintf ("init", "%d devices found\n", devcount);

  for (i = 0; i < devcount; i++)
    {
      struct device_info *devinfo = grub_uboot_dev_get (i);

      grub_dprintf ("init", "device handle: %d\n", i);
      grub_dprintf ("init", "  cookie\t= 0x%08x\n",
		    (grub_uint32_t) devinfo->cookie);

      if (devinfo->type & DEV_TYP_STOR)
	{
	  grub_dprintf ("init", "  type\t\t= DISK\n");
	  grub_ubootdisk_register (devinfo);
	}
      else if (devinfo->type & DEV_TYP_NET)
	{
	  /* Dealt with in ubootnet module. */
	  grub_dprintf ("init", "  type\t\t= NET (not supported yet)\n");
	}
      else
	{
	  grub_dprintf ("init", "%s: unknown device type", __FUNCTION__);
	}
    }

  return GRUB_ERR_NONE;
}

grub_err_t
grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
{
  int i;
  struct sys_info *si = grub_uboot_get_sys_info ();

  if (!si || (si->mr_no < 1))
    return GRUB_ERR_BUG;

  /* Iterate and call `hook'.  */
  for (i = 0; i < si->mr_no; i++)
    if ((si->mr[i].flags & MR_ATTR_MASK) == MR_ATTR_DRAM)
      hook (si->mr[i].start, si->mr[i].size, GRUB_MEMORY_AVAILABLE,
	    hook_data);

  return GRUB_ERR_NONE;
}