Permalink
Browse files

updated se, fixed fs open write, minor refactor

  • Loading branch information...
Reisyukaku committed Jul 29, 2018
1 parent 32a766f commit 0d21ce66d70fe377bdc053074fe0522c8ce0065e
Showing with 94 additions and 166 deletions.
  1. +4 −42 src/firmware.c
  2. +3 −1 src/fs.c
  3. +1 −0 src/hwinit.h
  4. +4 −94 src/hwinit/blz.c
  5. +2 −25 src/hwinit/blz.h
  6. +31 −0 src/hwinit/se.c
  7. +1 −0 src/hwinit/se.h
  8. +1 −1 src/hwinit/util.c
  9. +1 −1 src/hwinit/util.h
  10. +41 −0 src/package.c
  11. +5 −2 src/package.h
View
@@ -69,46 +69,6 @@ pk11_offs *pkg11_offsentify(u8 *pkg1) {
return NULL;
}
static u32 calcKipSize(pkg2_kip1_t *kip1) {
u32 size = sizeof(pkg2_kip1_t);
for (u32 j = 0; j < KIP1_NUM_SECTIONS; j++)
size += kip1->sections[j].size_comp;
return size;
}
void pkg2_parse_kips(link_t *info, pkg2_hdr_t *pkg2) {
u8 *ptr = pkg2->data + pkg2->sec_size[PKG2_SEC_KERNEL];
pkg2_ini1_t *ini1 = (pkg2_ini1_t *)ptr;
ptr += sizeof(pkg2_ini1_t);
for (u32 i = 0; i < ini1->num_procs; i++) {
pkg2_kip1_t *kip1 = (pkg2_kip1_t *)ptr;
pkg2_kip1_info_t *ki = (pkg2_kip1_info_t *)malloc(sizeof(pkg2_kip1_info_t));
ki->kip1 = kip1;
ki->size = calcKipSize(kip1);
list_append(info, &ki->link);
ptr += ki->size;
}
}
void loadKip(link_t *info, char *path) {
if(fopen(path, "rb") == 0) return;
pkg2_kip1_t *ckip = malloc(fsize());
fread(ckip, fsize(), 1);
fclose();
LIST_FOREACH_ENTRY(pkg2_kip1_info_t, ki, info, link) {
if (ki->kip1->tid == ckip->tid) {
ki->kip1 = ckip;
ki->size = calcKipSize(ckip);
return;
}
}
pkg2_kip1_info_t *ki = malloc(sizeof(pkg2_kip1_info_t));
ki->kip1 = ckip;
ki->size = calcKipSize(ckip);
list_append(info, &ki->link);
}
void patch(pk11_offs *pk11, pkg2_hdr_t *pkg2, link_t *kips) {
//Secmon patches
if(!customSecmon){
@@ -179,8 +139,10 @@ void patch(pk11_offs *pk11, pkg2_hdr_t *pkg2, link_t *kips) {
if(!customKern) {
//TODO
}
LIST_FOREACH_ENTRY(pkg2_kip1_info_t, ki, kips, link) {
if(ki->kip1->tid == 0x0100000000000001) {
LIST_FOREACH_ENTRY(pkg2_kip1_info_t, ki, kips, link) {
//Patch FS
if(ki->kip1->tid == 0x0100000000000000) {
print("Patching FS\n");
//TODO
}
}
View
@@ -41,7 +41,9 @@ u32 sd_mount() {
}
u32 fopen(const char *path, const char *mode) {
if (f_open(&fp, path, mode[0] == 'w' ? FA_WRITE : FA_READ) != FR_OK)
u32 m = (mode[0] == 0x77 ? (FA_WRITE|FA_CREATE_NEW) : FA_READ);
print("%kpath=%s;\tmode=%d\n%k", 0xFFF442DC, path, m, ORANGE);
if (f_open(&fp, path, m) != FR_OK)
return 0;
return 1;
}
View
@@ -18,6 +18,7 @@
#include <string.h>
#include "hwinit/btn.h"
#include "hwinit/blz.h"
#include "hwinit/clock.h"
#include "hwinit/cluster.h"
#include "hwinit/uart.h"
View
@@ -1,4 +1,5 @@
#include "blz.h"
#include "../package.h"
s64 Align(s64 data, s64 alignment){
return (data + alignment - 1) / alignment * alignment;
@@ -92,7 +93,7 @@ void slide(compress_info * info, const u8 * psrc, int size) {
}
int result = 0;
char * blz_compress(unsigned char * decompressed, u32 * isize) {
u8 * blz_compress(u8 *decompressed, u32 * isize) {
result = 1;
u8 * dest = malloc(*isize+1);
u32 classic_size = *isize;
@@ -208,13 +209,10 @@ char * blz_compress(unsigned char * decompressed, u32 * isize) {
return dest;
}
}
char * blz_decompress(unsigned char * compressed, u32 * isize) {
u32 size = *isize;
u8 * blz_decompress(u8 *compressed, u32 size) {
u32 compressed_size;
u32 init_index;
u32 uncompressed_addl_size;
@@ -275,93 +273,5 @@ char * blz_decompress(unsigned char * compressed, u32 * isize) {
break;
}
}
*isize = decompressed_size;
return decomp;
}
char * kip_comp(char * bytes, u32 * sz) {
kiphdr header;
kipseg * text_h;
kipseg * ro_h;
kipseg * data_h;
memcpy(&header, bytes, 0x100);
if(strncmp(header.magic, "KIP1", 4)) {
return NULL;
}
text_h = &header.segments[0];
ro_h = &header.segments[1];
data_h = &header.segments[2];
u32 toff = sizeof(kiphdr);
u32 roff = toff + text_h->filesize;
u32 doff = roff + ro_h->filesize;
u32 bsssize;
memcpy(&bsssize, bytes+0x18, 4);
char * text = malloc(text_h->filesize+1);
memcpy(text, bytes+toff, text_h->filesize);
char * ro = malloc(ro_h->filesize+1);
memcpy(ro, bytes+roff, ro_h->filesize);
char * data = malloc(data_h->filesize+1);
memcpy(data, bytes+doff, data_h->filesize);
text = blz_compress(text, &text_h->filesize);
ro = blz_compress(ro, &ro_h->filesize);
data = blz_compress(data, &data_h->filesize);
u32 totalsize = sizeof(kiphdr)+text_h->filesize+ro_h->filesize+data_h->filesize;
char * out = malloc(totalsize+1);
header.flags |= 7; //set first 3 bits to 1
memcpy(out, &header, sizeof(kiphdr));
memcpy(out+sizeof(kiphdr), text, text_h->filesize);
memcpy(out+sizeof(kiphdr)+text_h->filesize, ro, ro_h->filesize);
memcpy(out+sizeof(kiphdr)+text_h->filesize+ro_h->filesize,data, data_h->filesize);
*sz = totalsize;
return out;
}
char * kip_decomp(char * bytes, u32 * sz) {
kiphdr header;
kipseg * text_h;
kipseg * ro_h;
kipseg * data_h;
memcpy(&header, bytes, 0x100);
if(strncmp(header.magic, "KIP1", 4)) {
return NULL;
}
text_h = &header.segments[0];
ro_h = &header.segments[1];
data_h = &header.segments[2];
u32 toff = sizeof(kiphdr);
u32 roff = toff + text_h->filesize;
u32 doff = roff + ro_h->filesize;
u32 bsssize;
memcpy(&bsssize, bytes+0x18, 4);
char * text = malloc(text_h->filesize+1);
memcpy(text, bytes+toff, text_h->filesize);
char * ro = malloc(ro_h->filesize+1);
memcpy(ro, bytes+roff, ro_h->filesize);
char * data = malloc(data_h->filesize+1);
memcpy(data, bytes+doff, data_h->filesize);
text = blz_decompress(text, &text_h->filesize);
ro = blz_decompress(ro, &ro_h->filesize);
data = blz_decompress(data, &data_h->filesize);
u32 totalsize = sizeof(kiphdr)+text_h->filesize+ro_h->filesize+data_h->filesize;
char * out = malloc(totalsize+1);
header.flags &= ~7; //AND NOT 7 = zero first 3 bits
memcpy(out, &header, sizeof(kiphdr));
memcpy(out+sizeof(kiphdr), text, text_h->filesize);
memcpy(out+sizeof(kiphdr)+text_h->filesize, ro, ro_h->filesize);
memcpy(out+sizeof(kiphdr)+text_h->filesize+ro_h->filesize,data, data_h->filesize);
*sz = totalsize;
return out;
}
View
@@ -5,26 +5,6 @@
#include "types.h"
#include "heap.h"
typedef struct kipseg {
u32 loc;
u32 size;
u32 filesize;
} kipseg;
typedef struct {
u8 magic[4];
u8 name[0xC];
u64 title_id;
u32 process_category;
u8 thread_priority;
u8 cpu_id;
u8 unk;
u8 flags;
kipseg segments[6];
u32 capabilities[0x20];
} kiphdr;
typedef struct compress_info {
u16 windowpos;
u16 windowlen;
@@ -43,10 +23,7 @@ typedef struct compfooter {
int search(compress_info * info, const u8 * psrc, int * offset, int maxsize);
void slidebyte(compress_info * info, const u8 * psrc);
void slide(compress_info * info, const u8 * psrc, int size);
char * blz_decompress(unsigned char * compressed, u32 * isize);
char * blz_compress(unsigned char * decompressed, u32 * isize);
char * kip_decomp(char * bytes, u32 * sz);
char * kip_comp(char * bytes, u32 * sz);
u8 * blz_decompress(u8 *compressed, u32 size);
u8 * blz_compress(u8 *decompressed, u32 * isize);
#endif
View
@@ -1,5 +1,7 @@
/*
* Copyright (c) 2018 naehrwert
* Copyright (c) 2018 CTCaer
* Copyright (c) 2018 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
@@ -19,6 +21,7 @@
#include "heap.h"
#include "t210.h"
#include "se_t210.h"
#include "util.h"
typedef struct _se_ll_t
{
@@ -254,3 +257,31 @@ int se_aes_xts_crypt(u32 ks1, u32 ks2, u32 enc, u64 sec, void *dst, void *src, u
return 1;
}
// se_calc_sha256() was derived from Atmosphère's se_calculate_sha256.
int se_calc_sha256(void *dst, const void *src, u32 src_size)
{
int res;
// Setup config for SHA256, size = BITS(src_size).
SE(SE_CONFIG_REG_OFFSET) = SE_CONFIG_ENC_MODE(MODE_SHA256) | SE_CONFIG_ENC_ALG(ALG_SHA) | SE_CONFIG_DST(DST_HASHREG);
SE(SE_SHA_CONFIG_REG_OFFSET) = 1;
SE(SE_SHA_MSG_LENGTH_REG_OFFSET) = (u32)(src_size << 3);
SE(0x208) = 0;
SE(0x20C) = 0;
SE(0x210) = 0;
SE(SE_SHA_MSG_LEFT_REG_OFFSET) = (u32)(src_size << 3);
SE(0x218) = 0;
SE(0x21C) = 0;
SE(0x220) = 0;
// Trigger the operation.
res = _se_execute(OP_START, NULL, 0, src, src_size);
// Copy output hash.
u32 *dst32 = (u32 *)dst;
for (u32 i = 0; i < 8; i++)
dst32[i] = byte_swap_32(SE(SE_HASH_RESULT_REG_OFFSET + (i << 2)));
return res;
}
View
@@ -26,5 +26,6 @@ void se_aes_key_clear(u32 ks);
int se_aes_unwrap_key(u32 ks_dst, u32 ks_src, const void *input);
int se_aes_crypt_block_ecb(u32 ks, u32 enc, void *dst, const void *src);
int se_aes_crypt_ctr(u32 ks, void *dst, u32 dst_size, const void *src, u32 src_size, void *ctr);
int se_calc_sha256(void *dst, const void *src, u32 src_size);
#endif
View
@@ -62,4 +62,4 @@ uPtr memsearch(const u8 *startPos, u32 searchSize, const void *pattern, u32 patt
if (memcmp(pos, pattern, patternSize) == 0) return (uPtr)pos;
}
return 0;
}
}
View
@@ -33,4 +33,4 @@ void usleep(u32 microseconds);
void exec_cfg(u32 *base, const cfg_op_t *ops, u32 num_ops);
uPtr memsearch(const u8 *startPos, u32 searchSize, const void *pattern, u32 patternSize);
#endif
#endif
View
@@ -15,6 +15,7 @@
*/
#include "hwinit/gfx.h"
#include "hwinit/list.h"
#include "error.h"
#include "fs.h"
#include "package.h"
@@ -140,4 +141,44 @@ void buildFirmwarePackage(u8 *kernel, u32 kernel_size, link_t *kips_info) {
se_aes_crypt_ctr(8, hdr, sizeof(pkg2_hdr_t), hdr, sizeof(pkg2_hdr_t), hdr);
memset(hdr->ctr, 0 , 0x10);
*(u32 *)hdr->ctr = 0x100 + sizeof(pkg2_hdr_t) + kernel_size + ini1_size;
}
size_t calcKipSize(pkg2_kip1_t *kip1) {
u32 size = sizeof(pkg2_kip1_t);
for (u32 j = 0; j < KIP1_NUM_SECTIONS; j++)
size += kip1->sections[j].size_comp;
return size;
}
void pkg2_parse_kips(link_t *info, pkg2_hdr_t *pkg2) {
u8 *ptr = pkg2->data + pkg2->sec_size[PKG2_SEC_KERNEL];
pkg2_ini1_t *ini1 = (pkg2_ini1_t *)ptr;
ptr += sizeof(pkg2_ini1_t);
for (u32 i = 0; i < ini1->num_procs; i++) {
pkg2_kip1_t *kip1 = (pkg2_kip1_t *)ptr;
pkg2_kip1_info_t *ki = (pkg2_kip1_info_t *)malloc(sizeof(pkg2_kip1_info_t));
ki->kip1 = kip1;
ki->size = calcKipSize(kip1);
list_append(info, &ki->link);
ptr += ki->size;
}
}
void loadKip(link_t *info, char *path) {
if(fopen(path, "rb") == 0) return;
pkg2_kip1_t *ckip = malloc(fsize());
fread(ckip, fsize(), 1);
fclose();
LIST_FOREACH_ENTRY(pkg2_kip1_info_t, ki, info, link) {
if (ki->kip1->tid == ckip->tid) {
ki->kip1 = ckip;
ki->size = calcKipSize(ckip);
return;
}
}
pkg2_kip1_info_t *ki = malloc(sizeof(pkg2_kip1_info_t));
ki->kip1 = ckip;
ki->size = calcKipSize(ckip);
list_append(info, &ki->link);
}
View
@@ -48,7 +48,7 @@ typedef struct _pkg2_kip1_sec_t
typedef struct _pkg2_kip1_t
{
u32 magic;
u8 name[12];
char name[12];
u64 tid;
u32 proc_cat;
u8 main_thrd_prio;
@@ -91,4 +91,7 @@ typedef struct {
pkg2_hdr_t *unpackFirmwarePackage(u8 *data);
void pkg1_unpack(pk11_offs *offs, u8 *pkg1);
void buildFirmwarePackage(u8 *kernel, u32 kernel_size, link_t *kips_info);
void buildFirmwarePackage(u8 *kernel, u32 kernel_size, link_t *kips_info);
size_t calcKipSize(pkg2_kip1_t *kip1);
void pkg2_parse_kips(link_t *info, pkg2_hdr_t *pkg2);
void loadKip(link_t *info, char *path);

0 comments on commit 0d21ce6

Please sign in to comment.