|
1 | | -#include <string.h> |
2 | | -#include <stdio.h> |
3 | | -#include <stdbool.h> |
4 | | - |
5 | | -#include <switch.h> |
6 | | - |
7 | | -#define IRAM_PAYLOAD_MAX_SIZE 0x2F000 |
8 | | -#define IRAM_PAYLOAD_BASE 0x40010000 |
9 | | - |
10 | | -static alignas(0x1000) u8 g_reboot_payload[IRAM_PAYLOAD_MAX_SIZE]; |
11 | | -static alignas(0x1000) u8 g_ff_page[0x1000]; |
12 | | -static alignas(0x1000) u8 g_work_page[0x1000]; |
13 | | - |
14 | | -void do_iram_dram_copy(void *buf, uintptr_t iram_addr, size_t size, int option) { |
15 | | - memcpy(g_work_page, buf, size); |
16 | | - |
17 | | - SecmonArgs args = {0}; |
18 | | - args.X[0] = 0xF0000201; /* smcAmsIramCopy */ |
19 | | - args.X[1] = (uintptr_t)g_work_page; /* DRAM Address */ |
20 | | - args.X[2] = iram_addr; /* IRAM Address */ |
21 | | - args.X[3] = size; /* Copy size */ |
22 | | - args.X[4] = option; /* 0 = Read, 1 = Write */ |
23 | | - svcCallSecureMonitor(&args); |
24 | | - |
25 | | - memcpy(buf, g_work_page, size); |
26 | | -} |
27 | | - |
28 | | -void copy_to_iram(uintptr_t iram_addr, void *buf, size_t size) { |
29 | | - do_iram_dram_copy(buf, iram_addr, size, 1); |
30 | | -} |
31 | | - |
32 | | -void copy_from_iram(void *buf, uintptr_t iram_addr, size_t size) { |
33 | | - do_iram_dram_copy(buf, iram_addr, size, 0); |
34 | | -} |
35 | | - |
36 | | -static void clear_iram(void) { |
37 | | - memset(g_ff_page, 0xFF, sizeof(g_ff_page)); |
38 | | - for (size_t i = 0; i < IRAM_PAYLOAD_MAX_SIZE; i += sizeof(g_ff_page)) { |
39 | | - copy_to_iram(IRAM_PAYLOAD_BASE + i, g_ff_page, sizeof(g_ff_page)); |
40 | | - } |
41 | | -} |
42 | | - |
43 | | -static void reboot_to_payload(void) { |
44 | | - clear_iram(); |
45 | | - |
46 | | - for (size_t i = 0; i < IRAM_PAYLOAD_MAX_SIZE; i += 0x1000) { |
47 | | - copy_to_iram(IRAM_PAYLOAD_BASE + i, &g_reboot_payload[i], 0x1000); |
48 | | - } |
49 | | - |
50 | | - splSetConfig((SplConfigItem)65001, 2); |
51 | | -} |
52 | | - |
53 | | -int main(int argc, char **argv) |
54 | | -{ |
55 | | - |
56 | | - bool can_reboot = true; |
57 | | - Result rc = splInitialize(); |
58 | | - if (R_FAILED(rc)) { |
59 | | - consoleInit(NULL); |
60 | | - printf("Failed to initialize spl: 0x%x\n", rc); |
61 | | - can_reboot = false; |
62 | | - } else { |
63 | | - FILE *f = fopen("sdmc:/hekate.bin", "rb"); |
64 | | - if (f == NULL) { |
65 | | - |
66 | | - consoleInit(NULL); |
67 | | - printf("Failed to open hekate.bin!\n"); |
68 | | - can_reboot = false; |
69 | | - } else { |
70 | | - fread(g_reboot_payload, 1, sizeof(g_reboot_payload), f); |
71 | | - fclose(f); |
72 | | - } |
73 | | - } |
74 | | - // Main loop |
75 | | - while(appletMainLoop()) |
76 | | - {if (can_reboot) { |
77 | | - reboot_to_payload(); |
78 | | - } |
79 | | - } |
80 | | - |
81 | | - if (can_reboot) { |
82 | | - splExit(); |
83 | | - } |
84 | | - |
85 | | - consoleExit(NULL); |
86 | | - return 0; |
87 | | -} |
88 | | - |
| 1 | +#include <string.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdbool.h> |
| 4 | + |
| 5 | +#include <switch.h> |
| 6 | + |
| 7 | +#define IRAM_PAYLOAD_MAX_SIZE 0x2F000 |
| 8 | +#define IRAM_PAYLOAD_BASE 0x40010000 |
| 9 | + |
| 10 | +static alignas(0x1000) u8 g_reboot_payload[IRAM_PAYLOAD_MAX_SIZE]; |
| 11 | +static alignas(0x1000) u8 g_ff_page[0x1000]; |
| 12 | +static alignas(0x1000) u8 g_work_page[0x1000]; |
| 13 | + |
| 14 | +void do_iram_dram_copy(void *buf, uintptr_t iram_addr, size_t size, int option) { |
| 15 | + memcpy(g_work_page, buf, size); |
| 16 | + |
| 17 | + SecmonArgs args = {0}; |
| 18 | + args.X[0] = 0xF0000201; /* smcAmsIramCopy */ |
| 19 | + args.X[1] = (uintptr_t)g_work_page; /* DRAM Address */ |
| 20 | + args.X[2] = iram_addr; /* IRAM Address */ |
| 21 | + args.X[3] = size; /* Copy size */ |
| 22 | + args.X[4] = option; /* 0 = Read, 1 = Write */ |
| 23 | + svcCallSecureMonitor(&args); |
| 24 | + |
| 25 | + memcpy(buf, g_work_page, size); |
| 26 | +} |
| 27 | + |
| 28 | +void copy_to_iram(uintptr_t iram_addr, void *buf, size_t size) { |
| 29 | + do_iram_dram_copy(buf, iram_addr, size, 1); |
| 30 | +} |
| 31 | + |
| 32 | +void copy_from_iram(void *buf, uintptr_t iram_addr, size_t size) { |
| 33 | + do_iram_dram_copy(buf, iram_addr, size, 0); |
| 34 | +} |
| 35 | + |
| 36 | +static void clear_iram(void) { |
| 37 | + memset(g_ff_page, 0xFF, sizeof(g_ff_page)); |
| 38 | + for (size_t i = 0; i < IRAM_PAYLOAD_MAX_SIZE; i += sizeof(g_ff_page)) { |
| 39 | + copy_to_iram(IRAM_PAYLOAD_BASE + i, g_ff_page, sizeof(g_ff_page)); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +static void reboot_to_payload(void) { |
| 44 | + clear_iram(); |
| 45 | + |
| 46 | + for (size_t i = 0; i < IRAM_PAYLOAD_MAX_SIZE; i += 0x1000) { |
| 47 | + copy_to_iram(IRAM_PAYLOAD_BASE + i, &g_reboot_payload[i], 0x1000); |
| 48 | + } |
| 49 | + |
| 50 | + splSetConfig((SplConfigItem)65001, 2); |
| 51 | +} |
| 52 | + |
| 53 | +int main(int argc, char **argv) |
| 54 | +{ |
| 55 | + |
| 56 | + bool can_reboot = true; |
| 57 | + Result rc = splInitialize(); |
| 58 | + if (R_FAILED(rc)) { |
| 59 | + consoleInit(NULL); |
| 60 | + printf("Failed to initialize spl: 0x%x\n", rc); |
| 61 | + can_reboot = false; |
| 62 | + } else { |
| 63 | + FILE *f = fopen("sdmc:/hekate.bin", "rb"); |
| 64 | + if (f == NULL) { |
| 65 | + consoleInit(NULL); |
| 66 | + FILE *c = fopen("sdmc:/bootloader/update.bin", "rb"); |
| 67 | + if (c == NULL) { |
| 68 | + |
| 69 | + consoleInit(NULL); |
| 70 | + printf("Failed to open update.bin!\n"); |
| 71 | + can_reboot = false; |
| 72 | + } else { |
| 73 | + fread(g_reboot_payload, 1, sizeof(g_reboot_payload), c); |
| 74 | + fclose(c); |
| 75 | + } |
| 76 | + |
| 77 | + } else { |
| 78 | + fread(g_reboot_payload, 1, sizeof(g_reboot_payload), f); |
| 79 | + fclose(f); |
| 80 | + } |
| 81 | + } |
| 82 | + // Main loop |
| 83 | + while(appletMainLoop()) |
| 84 | + {if (can_reboot) { |
| 85 | + reboot_to_payload(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (can_reboot) { |
| 90 | + splExit(); |
| 91 | + } |
| 92 | + |
| 93 | + consoleExit(NULL); |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
0 commit comments