File: platform.c

package info (click to toggle)
grub2 2.02%2Bdfsg1-20
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 61,784 kB
  • sloc: ansic: 384,086; asm: 16,323; sh: 13,230; cpp: 1,994; makefile: 1,488; python: 1,458; sed: 423; lex: 393; yacc: 267; awk: 75; lisp: 50; perl: 31
file content (254 lines) | stat: -rw-r--r-- 5,780 bytes parent folder | download
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 *  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 <config.h>

#include <grub/util/install.h>
#include <grub/emu/config.h>
#include <grub/emu/exec.h>
#include <grub/emu/misc.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/utsname.h>

static int
is_not_empty_directory (const char *dir)
{
  DIR *d;
  struct dirent *de;

  d = opendir (dir);
  if (!d)
    return 0;
  while ((de = readdir (d)))
    {
      if (strcmp (de->d_name, ".") == 0
	  || strcmp (de->d_name, "..") == 0)
	continue;
      closedir (d);
      return 1;
    }

  closedir (d);
  return 0;
}

static int
is_64_kernel (void)
{
  struct utsname un;

  if (uname (&un) < 0)
    return 0;

  return strcmp (un.machine, "x86_64") == 0;
}

static int
read_platform_size (void)
{
  FILE *fp;
  char *buf = NULL;
  size_t len = 0;
  int ret = 0;

  /* Newer kernels can tell us directly about the size of the
   * underlying firmware - let's see if that interface is there. */
  fp = grub_util_fopen ("/sys/firmware/efi/fw_platform_size", "r");
  if (fp != NULL)
  {
    if (getline (&buf, &len, fp) >= 3) /* 2 digits plus newline */
      {
	if (strncmp (buf, "32", 2) == 0)
	  ret = 32;
	else if (strncmp (buf, "64", 2) == 0)
	  ret = 64;
      }
    free (buf);
    fclose (fp);
  }

  if (ret == 0)
    {
      /* Unrecognised - fall back to matching the kernel size
       * instead */
      if (is_64_kernel ())
	ret = 64;
      else
	ret = 32;
    }

  return ret;
}

const char *
grub_install_get_default_arm_platform (void)
{
  /*
   * On Linux, we need the efivars kernel modules. If no EFI is available this
   * module just does nothing besides a small hello and if we detect efi we'll
   * load it anyway later. So it should be safe to try to load it here.
   */
  grub_util_exec_redirect_all ((const char * []){ "modprobe", "efivars", NULL },
			       NULL, NULL, "/dev/null");

  grub_util_info ("Looking for /sys/firmware/efi ..");
  if (is_not_empty_directory ("/sys/firmware/efi"))
    {
      const char *pkglibdir = grub_util_get_pkglibdir ();
      const char *platform;
      char *pd;
      int found;

      grub_util_info ("...found");
      platform = "arm-efi";

      pd = grub_util_path_concat (2, pkglibdir, platform);
      found = grub_util_is_directory (pd);
      free (pd);
      if (found)
	return platform;
      else
	grub_util_info ("... but %s platform not available", platform);
    }

  grub_util_info ("... not found");
  return "arm-uboot";
}

const char *
grub_install_get_default_x86_platform (void)
{ 
  /*
     On Linux, we need the efivars kernel modules.
     If no EFI is available this module just does nothing
     besides a small hello and if we detect efi we'll load it
     anyway later. So it should be safe to
     try to load it here.
   */
  grub_util_exec_redirect_all ((const char * []){ "modprobe", "efivars", NULL },
			       NULL, NULL, "/dev/null");

  grub_util_info ("Looking for /sys/firmware/efi ..");
  if (is_not_empty_directory ("/sys/firmware/efi"))
    {
      const char *pkglibdir = grub_util_get_pkglibdir ();
      const char *platform;
      char *pd;
      int found;

      grub_util_info ("...found");
      if (read_platform_size() == 64)
	platform = "x86_64-efi";
      else
	platform = "i386-efi";

      pd = grub_util_path_concat (2, pkglibdir, platform);
      found = grub_util_is_directory (pd);
      free (pd);
      if (found)
	return platform;
      else
	grub_util_info ("... but %s platform not available", platform);
    }

  grub_util_info ("... not found. Looking for /proc/device-tree ..");
  if (is_not_empty_directory ("/proc/device-tree"))
    {
      grub_util_info ("...found");
      return "i386-ieee1275";
    }

  grub_util_info ("... not found");
  return "i386-pc";
}

const char *
grub_install_get_default_powerpc_machtype (void)
{
  FILE *fp;
  char *buf = NULL;
  size_t len = 0;
  const char *machtype = "generic";

  fp = grub_util_fopen ("/proc/cpuinfo", "r");
  if (! fp)
    return machtype;

  while (getline (&buf, &len, fp) > 0)
    {
      if (strncmp (buf, "pmac-generation",
		   sizeof ("pmac-generation") - 1) == 0)
	{
	  if (strstr (buf, "NewWorld"))
	    {
	      machtype = "pmac_newworld";
	      break;
	    }
	  if (strstr (buf, "OldWorld"))
	    {
	      machtype = "pmac_oldworld";
	      break;
	    }
	}

      if (strncmp (buf, "motherboard", sizeof ("motherboard") - 1) == 0 &&
	  strstr (buf, "AAPL"))
	{
	  machtype = "pmac_oldworld";
	  break;
	}

      if (strncmp (buf, "machine", sizeof ("machine") - 1) == 0 &&
	  strstr (buf, "CHRP IBM"))
	{
	  if (strstr (buf, "qemu"))
	    {
	      machtype = "chrp_ibm_qemu";
	      break;
	    }
	  else
	    {
	      machtype = "chrp_ibm";
	      break;
	    }
	}

      if (strncmp (buf, "platform", sizeof ("platform") - 1) == 0)
	{
	  if (strstr (buf, "Maple"))
	    {
	      machtype = "maple";
	      break;
	    }
	  if (strstr (buf, "Cell"))
	    {
	      machtype = "cell";
	      break;
	    }
	}
    }

  free (buf);
  fclose (fp);
  return machtype;
}