2018-09-07 15:00:13 +00:00
|
|
|
/*
|
2019-04-08 02:00:49 +00:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2018-09-07 15:00:13 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-03-15 15:14:41 +00:00
|
|
|
#include <stdbool.h>
|
2018-05-20 14:18:48 +00:00
|
|
|
#include <stdarg.h>
|
2018-03-15 15:14:41 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "se.h"
|
|
|
|
#include "fuse.h"
|
|
|
|
#include "pmc.h"
|
2018-05-20 21:52:09 +00:00
|
|
|
#include "car.h"
|
2018-03-15 15:14:41 +00:00
|
|
|
#include "timers.h"
|
2018-08-18 16:59:33 +00:00
|
|
|
#include "btn.h"
|
2018-05-20 14:18:48 +00:00
|
|
|
#include "panic.h"
|
2018-09-26 00:13:20 +00:00
|
|
|
#include "lib/log.h"
|
2018-03-15 15:14:41 +00:00
|
|
|
|
2018-05-16 17:58:04 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <inttypes.h>
|
2018-03-15 15:14:41 +00:00
|
|
|
|
2019-01-26 08:21:34 +00:00
|
|
|
#define u8 uint8_t
|
|
|
|
#define u32 uint32_t
|
|
|
|
#include "fusee_primary_bin.h"
|
2019-02-20 12:52:11 +00:00
|
|
|
#include "sept_primary_bin.h"
|
2019-01-26 08:21:34 +00:00
|
|
|
#include "rebootstub_bin.h"
|
|
|
|
#undef u8
|
|
|
|
#undef u32
|
|
|
|
|
2018-07-29 16:17:51 +00:00
|
|
|
void wait(uint32_t microseconds) {
|
|
|
|
uint32_t old_time = TIMERUS_CNTR_1US_0;
|
|
|
|
while (TIMERUS_CNTR_1US_0 - old_time <= microseconds) {
|
|
|
|
/* Spin-lock. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 12:11:46 +00:00
|
|
|
__attribute__((noreturn)) void watchdog_reboot(void) {
|
|
|
|
volatile watchdog_timers_t *wdt = GET_WDT(4);
|
|
|
|
wdt->PATTERN = WDT_REBOOT_PATTERN;
|
|
|
|
wdt->COMMAND = 2; /* Disable Counter. */
|
|
|
|
GET_WDT_REBOOT_CFG_REG(4) = 0xC0000000;
|
|
|
|
wdt->CONFIG = 0x8019; /* Full System Reset after Fourth Counter expires, using TIMER(9). */
|
|
|
|
wdt->COMMAND = 1; /* Enable Counter. */
|
|
|
|
while (true) {
|
|
|
|
/* Wait for reboot. */
|
2018-03-15 15:14:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 12:11:46 +00:00
|
|
|
__attribute__((noreturn)) void pmc_reboot(uint32_t scratch0) {
|
|
|
|
APBDEV_PMC_SCRATCH0_0 = scratch0;
|
|
|
|
|
|
|
|
/* Reset the processor. */
|
|
|
|
APBDEV_PMC_CONTROL = BIT(4);
|
|
|
|
while (true) {
|
|
|
|
/* Wait for reboot. */
|
|
|
|
}
|
2018-03-15 15:14:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 12:52:11 +00:00
|
|
|
__attribute__((noreturn)) static void reboot_to_payload(void) {
|
2019-01-26 08:21:34 +00:00
|
|
|
/* Patch SDRAM init to perform an SVC immediately after second write */
|
|
|
|
APBDEV_PMC_SCRATCH45_0 = 0x2E38DFFF;
|
|
|
|
APBDEV_PMC_SCRATCH46_0 = 0x6001DC28;
|
|
|
|
/* Set SVC handler to jump to reboot stub in IRAM. */
|
|
|
|
APBDEV_PMC_SCRATCH33_0 = 0x4003F000;
|
|
|
|
APBDEV_PMC_SCRATCH40_0 = 0x6000F208;
|
|
|
|
|
2019-02-20 12:52:11 +00:00
|
|
|
/* Copy reboot stub into IRAM high. */
|
|
|
|
for (size_t i = 0; i < rebootstub_bin_size; i += sizeof(uint32_t)) {
|
|
|
|
write32le((void *)0x4003F000, i, read32le(rebootstub_bin, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trigger warm reboot. */
|
|
|
|
pmc_reboot(1 << 0);
|
|
|
|
while (true) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((noreturn)) void reboot_to_fusee_primary(void) {
|
2019-01-26 08:21:34 +00:00
|
|
|
/* Copy fusee-primary into IRAM low. */
|
|
|
|
for (size_t i = 0; i < fusee_primary_bin_size; i += sizeof(uint32_t)) {
|
|
|
|
write32le((void *)0x40010000, i, read32le(fusee_primary_bin, i));
|
|
|
|
}
|
|
|
|
|
2019-02-20 12:52:11 +00:00
|
|
|
reboot_to_payload();
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((noreturn)) void reboot_to_sept(const void *tsec_fw, size_t tsec_fw_length, const void *stage2, size_t stage2_size) {
|
|
|
|
|
|
|
|
/* Copy tsec firmware. */
|
|
|
|
for (size_t i = 0; i < tsec_fw_length; i += sizeof(uint32_t)) {
|
|
|
|
write32le((void *)0x40010F00, i, read32le(tsec_fw, i));
|
|
|
|
}
|
|
|
|
MAKE_REG32(0x40010EFC) = tsec_fw_length;
|
|
|
|
|
|
|
|
/* Copy stage 2. */
|
|
|
|
for (size_t i = 0; i < stage2_size; i += sizeof(uint32_t)) {
|
|
|
|
write32le((void *)0x40016FE0, i, read32le(stage2, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy sept into IRAM low. */
|
|
|
|
for (size_t i = 0; i < sept_primary_bin_size; i += sizeof(uint32_t)) {
|
|
|
|
write32le((void *)0x4003F000, i, read32le(sept_primary_bin, i));
|
2019-01-26 08:21:34 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 12:52:11 +00:00
|
|
|
/* Patch SDRAM init to perform an SVC immediately after second write */
|
|
|
|
APBDEV_PMC_SCRATCH45_0 = 0x2E38DFFF;
|
|
|
|
APBDEV_PMC_SCRATCH46_0 = 0x6001DC28;
|
|
|
|
/* Set SVC handler to jump to reboot stub in IRAM. */
|
|
|
|
APBDEV_PMC_SCRATCH33_0 = 0x4003F000;
|
|
|
|
APBDEV_PMC_SCRATCH40_0 = 0x6000F208;
|
|
|
|
|
2019-01-26 08:21:34 +00:00
|
|
|
/* Trigger warm reboot. */
|
|
|
|
pmc_reboot(1 << 0);
|
2019-02-20 12:52:11 +00:00
|
|
|
while (true) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((noreturn)) void reboot_to_iram_payload(void *payload, size_t payload_size) {
|
|
|
|
/* Copy sept into IRAM low. */
|
|
|
|
for (size_t i = 0; i < payload_size; i += sizeof(uint32_t)) {
|
|
|
|
write32le((void *)0x40010000, i, read32le(payload, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
reboot_to_payload();
|
2019-01-26 08:21:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 21:52:09 +00:00
|
|
|
__attribute__((noreturn)) void wait_for_button_and_reboot(void) {
|
2018-05-20 12:11:46 +00:00
|
|
|
uint32_t button;
|
|
|
|
while (true) {
|
|
|
|
button = btn_read();
|
|
|
|
if (button & BTN_POWER) {
|
2019-01-26 08:21:34 +00:00
|
|
|
reboot_to_fusee_primary();
|
2018-05-20 12:11:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-15 15:14:41 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:22:25 +00:00
|
|
|
void wait_for_button(void) {
|
|
|
|
uint32_t button;
|
|
|
|
while (true) {
|
|
|
|
button = btn_read();
|
|
|
|
if (button) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 12:11:46 +00:00
|
|
|
__attribute__ ((noreturn)) void generic_panic(void) {
|
2018-05-20 14:18:48 +00:00
|
|
|
panic(0xFF000006);
|
2018-05-20 12:11:46 +00:00
|
|
|
}
|
2018-05-20 14:18:48 +00:00
|
|
|
|
|
|
|
__attribute__((noreturn)) void fatal_error(const char *fmt, ...) {
|
|
|
|
va_list args;
|
2018-09-23 21:22:25 +00:00
|
|
|
print(SCREEN_LOG_LEVEL_ERROR, "Fatal error: ");
|
2018-05-20 14:18:48 +00:00
|
|
|
va_start(args, fmt);
|
2018-09-23 21:22:25 +00:00
|
|
|
vprint(SCREEN_LOG_LEVEL_ERROR, fmt, args);
|
2018-05-20 14:18:48 +00:00
|
|
|
va_end(args);
|
2018-09-25 21:43:52 +00:00
|
|
|
print(SCREEN_LOG_LEVEL_ERROR | SCREEN_LOG_LEVEL_NO_PREFIX, "\n Press POWER to reboot.\n");
|
2018-05-20 21:52:09 +00:00
|
|
|
wait_for_button_and_reboot();
|
2018-05-20 14:18:48 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 15:14:41 +00:00
|
|
|
__attribute__((noinline)) bool overlaps(uint64_t as, uint64_t ae, uint64_t bs, uint64_t be)
|
|
|
|
{
|
2019-04-20 10:41:03 +00:00
|
|
|
if(as <= bs && bs < ae)
|
2018-03-15 15:14:41 +00:00
|
|
|
return true;
|
2019-04-20 10:41:03 +00:00
|
|
|
if(bs <= as && as < be)
|
2018-03-15 15:14:41 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-16 17:58:04 +00:00
|
|
|
|
|
|
|
/* Adapted from https://gist.github.com/ccbrown/9722406 */
|
|
|
|
void hexdump(const void* data, size_t size, uintptr_t addrbase) {
|
|
|
|
const uint8_t *d = (const uint8_t *)data;
|
|
|
|
char ascii[17];
|
|
|
|
ascii[16] = '\0';
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
if (i % 16 == 0) {
|
|
|
|
printf("%0*" PRIXPTR ": | ", 2 * sizeof(addrbase), addrbase + i);
|
|
|
|
}
|
|
|
|
printf("%02X ", d[i]);
|
|
|
|
if (d[i] >= ' ' && d[i] <= '~') {
|
|
|
|
ascii[i % 16] = d[i];
|
|
|
|
} else {
|
|
|
|
ascii[i % 16] = '.';
|
|
|
|
}
|
|
|
|
if ((i+1) % 8 == 0 || i+1 == size) {
|
|
|
|
printf(" ");
|
|
|
|
if ((i+1) % 16 == 0) {
|
|
|
|
printf("| %s \n", ascii);
|
|
|
|
} else if (i+1 == size) {
|
|
|
|
ascii[(i+1) % 16] = '\0';
|
|
|
|
if ((i+1) % 16 <= 8) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
for (size_t j = (i+1) % 16; j < 16; j++) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
printf("| %s \n", ascii);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|