-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgrub2-add-setup_var-cmd.patch
248 lines (246 loc) · 7.87 KB
/
grub2-add-setup_var-cmd.patch
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
diff -Nur grub2-1.96+20090709/commands/efi/setup_var.c /usr/src/grub2-1.96+20090709/commands/efi/setup_var.c
--- grub2-1.96+20090709/commands/efi/setup_var.c 1970-01-01 01:00:00.000000000 +0100
+++ /usr/src/grub2-1.96+20090709/commands/efi/setup_var.c 2009-07-19 01:52:50.716462005 +0200
@@ -0,0 +1,220 @@
+/* setup_var.c - InsydeH2o Setup variable modification tool, can modify single
+ * bytes within the Setup variable */
+/* (c) 2009 by Bernhard Froemel
+ *
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2008 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 <stdint.h>
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/file.h>
+#include <grub/efi/efi.h>
+#include <grub/pci.h>
+#include <grub/command.h>
+
+#define INSYDE_SETUP_VAR ((grub_efi_char16_t*)"S\0e\0t\0u\0p\0\0\0")
+#define INSYDE_SETUP_VAR_NSIZE (12)
+#define INSYDE_SETUP_VAR_SIZE (0x2bc)
+#define INSYDE_SETUP_VAR_GUID { 0xa04a27f4, 0xdf00, 0x4d42, { 0xb5, 0x52, 0x39, 0x51, 0x13, 0x02, 0x11, 0x3d } }
+#define MAX_VARIABLE_SIZE (1024)
+
+
+void print_varname(grub_efi_char16_t* str);
+
+void print_varname(grub_efi_char16_t* str)
+{
+ while(*str != 0x0)
+ {
+ grub_printf("%c", (uint8_t) *str);
+ str++;
+ }
+}
+
+static grub_err_t
+grub_cmd_setup_var (grub_command_t cmd __attribute__ ((unused)),
+ int argc, char *argv[])
+{
+ grub_efi_status_t status;
+ grub_efi_guid_t setup_var_guid = INSYDE_SETUP_VAR_GUID;
+ grub_efi_guid_t guid;
+ uint8_t tmp_data[1024];
+ uint16_t offset = 0x1af;
+ grub_efi_uintn_t setup_var_size = INSYDE_SETUP_VAR_SIZE;
+ uint8_t set_value = 0x0;
+ grub_efi_uint32_t setup_var_attr = 0x7;
+ char* endptr;
+
+ grub_efi_char16_t name[MAX_VARIABLE_SIZE/2];
+ grub_efi_uintn_t name_size;
+
+ grub_printf("Hello!\n"
+ );
+ grub_printf(
+"You may brick your InsydeH2o based laptop and need to send it in\n"
+ );
+ grub_printf(
+"for repair.\n");
+ grub_printf(
+"Some vendors (like Sony) are rather slow, and it could get expensive if they\n"
+ );
+ grub_printf(
+"discover that you used this or similar tools.\nYou should be *very* sure what you do.\n"
+ );
+ grub_printf(
+"\n\nThis Setup variable modification tool may only work with current (July 2009)\n"
+ );
+ grub_printf(
+"Insyde H2o based firmware releases. Vendors may decide to lock the EFI\n"
+ );
+ grub_printf(
+"environment completely out or introduce other security measures.\n"
+ );
+ grub_printf(
+"Final warning: YOU MAY BRICK YOUR LAPTOP IF YOU USE THIS TOOL - I TAKE N O \n"
+ );
+ grub_printf(
+"RESPONSIBILITY FOR Y O U R ACTIONS.\n"
+ );
+ grub_printf(
+"\n\n(c) 2009 by Bernhard Froemel <bfroemel@gmail.com>\n"
+ );
+
+ name[0] = 0x0;
+ /* scan for Setup variable */
+ grub_printf("Looking for Setup variable...\n");
+ do
+ {
+ name_size = MAX_VARIABLE_SIZE;
+ status = efi_call_3(grub_efi_system_table->runtime_services->get_next_variable_name,
+ &name_size,
+ name,
+ &guid);
+
+ if(status == GRUB_EFI_NOT_FOUND)
+ { /* finished traversing VSS */
+ break;
+ }
+
+ if(status)
+ {
+ grub_printf("status: 0x%02x\n", (uint32_t) status);
+ }
+ if(! status && name_size == INSYDE_SETUP_VAR_NSIZE && 0 == grub_memcmp(name, INSYDE_SETUP_VAR, name_size))
+ {
+ grub_printf("var name: ");
+ print_varname(name);
+ grub_printf(", var size: %u, var guid: %08x-%04x-%04x - %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\n\n",
+ (uint32_t) name_size,
+ guid.data1,
+ guid.data2,
+ guid.data3,
+ guid.data4[0], guid.data4[1], guid.data4[2], guid.data4[3], guid.data4[4], guid.data4[5], guid.data4[6], guid.data4[7]
+ );
+
+ grub_printf("--> found variable \"Setup\"!\n");
+ if(grub_memcmp(&guid, &setup_var_guid, sizeof(grub_efi_guid_t)) == 0)
+ {
+ grub_printf("--> GUID matches expected GUID\n");
+ }
+ else
+ {
+ grub_printf("--> GUID does not match expected GUID, taking it nevertheless...\n");
+ grub_memcpy(&setup_var_guid, &guid, sizeof(grub_efi_guid_t));
+ }
+ }
+ } while (! status);
+
+ /* obtain current contents of Setup variable */
+ if(argc >= 1 && argc < 3)
+ {
+ grub_errno = 0;
+ offset = grub_strtoul(argv[0], &endptr, 16);
+ if(endptr == argv[0] || grub_errno != 0)
+ {
+ return grub_error(GRUB_ERR_BAD_ARGUMENT, "can't decode your first argument. Please provide a hex value (e.g. 0x1af).");
+ }
+ status = efi_call_5(grub_efi_system_table->runtime_services->get_variable,
+ INSYDE_SETUP_VAR,
+ &setup_var_guid,
+ &setup_var_attr,
+ &setup_var_size,
+ tmp_data);
+ if(status == GRUB_EFI_BUFFER_TOO_SMALL)
+ {
+ grub_printf("expected a different size of the Setup variable. Continue with care...\n");
+ status = efi_call_5(grub_efi_system_table->runtime_services->get_variable,
+ INSYDE_SETUP_VAR,
+ &setup_var_guid,
+ &setup_var_attr,
+ &setup_var_size,
+ tmp_data);
+ }
+ if(status)
+ {
+ return grub_error(GRUB_ERR_INVALID_COMMAND, "can't get variable using efi (error: 0x%016x)", status);
+ }
+ grub_printf("successfully obtained \"Setup\" variable from VSS.\n");
+ if(offset > setup_var_size)
+ {
+ return grub_error(GRUB_ERR_BAD_ARGUMENT, "offset is out of range.");
+ }
+ grub_printf("offset 0x%02x is: 0x%02x\n", offset, tmp_data[offset]);
+ }
+ /* modify and write Setup variable, if user requests it */
+ if(argc == 2)
+ {
+ set_value = grub_strtoul(argv[1], &endptr, 16);
+ if(endptr == argv[1] || grub_errno != 0)
+ {
+ return grub_error(GRUB_ERR_BAD_ARGUMENT, "can't decode your second argument. Please provide a hex value (e.g. 0x01).");
+ }
+ grub_printf("setting offset 0x%02x to 0x%02x\n", offset, set_value);
+ tmp_data[offset] = set_value;
+ status = efi_call_5(grub_efi_system_table->runtime_services->set_variable,
+ INSYDE_SETUP_VAR,
+ &setup_var_guid,
+ setup_var_attr,
+ setup_var_size,
+ tmp_data);
+ if(status)
+ {
+ return grub_error(GRUB_ERR_INVALID_COMMAND, "can't set variable using efi (error: 0x%016x)", status);
+ }
+ }
+
+ if(argc == 0 || argc > 2)
+ {
+ return grub_error(GRUB_ERR_BAD_ARGUMENT, "Usage: setup_var offset [setval]");
+ }
+ return grub_errno;
+}
+
+
+static grub_command_t cmd_setup_var;
+
+GRUB_MOD_INIT(setup_var)
+{
+ cmd_setup_var = grub_register_command ("setup_var", grub_cmd_setup_var,
+ "setup_var offset [setval]",
+ "Read/Write specific (byte) offset of setup variable.");
+}
+
+GRUB_MOD_FINI(setup_var)
+{
+ grub_unregister_command (cmd_setup_var);
+}
diff -Nur grub2-1.96+20090709/conf/x86_64-efi.rmk /usr/src/grub2-1.96+20090709/conf/x86_64-efi.rmk
--- grub2-1.96+20090709/conf/x86_64-efi.rmk 2009-07-01 15:01:33.000000000 +0200
+++ /usr/src/grub2-1.96+20090709/conf/x86_64-efi.rmk 2009-07-18 10:16:49.398681405 +0200
@@ -80,7 +80,7 @@
pkglib_MODULES = kernel.mod chain.mod appleldr.mod \
halt.mod reboot.mod linux.mod pci.mod lspci.mod \
datetime.mod date.mod datehook.mod loadbios.mod \
- fixvideo.mod mmap.mod acpi.mod
+ fixvideo.mod mmap.mod acpi.mod setup_var.mod
# For kernel.mod.
kernel_mod_EXPORTS = no
@@ -192,6 +192,11 @@
fixvideo_mod_CFLAGS = $(COMMON_CFLAGS)
fixvideo_mod_LDFLAGS = $(COMMON_LDFLAGS)
+# For setup_var.mod
+setup_var_mod_SOURCES = commands/efi/setup_var.c
+setup_var_mod_CFLAGS = $(COMMON_CFLAGS)
+setup_var_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
pkglib_MODULES += xnu.mod
xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c loader/i386/efi/xnu.c\
loader/macho.c loader/xnu.c loader/i386/xnu_helper.S