1
0
Fork 0
mirror of https://github.com/Scandal-UK/Incognito_RCM.git synced 2024-11-22 20:06:42 +00:00

add retry and debug functions

This commit is contained in:
jimzrt 2019-10-07 19:26:40 +02:00
parent 5de9cf32a7
commit b6c552243a
4 changed files with 114 additions and 101 deletions

View file

@ -414,6 +414,7 @@ void gfx_print_header()
gfx_con.fntsz = prevFontSize; gfx_con.fntsz = prevFontSize;
} }
#ifdef DEBUG
void gfx_hexdump(u32 base, const u8 *buf, u32 len) void gfx_hexdump(u32 base, const u8 *buf, u32 len)
{ {
if (gfx_con.mute) if (gfx_con.mute)
@ -467,87 +468,6 @@ void gfx_hexdump(u32 base, const u8 *buf, u32 len)
gfx_con.fntsz = prevFontSize; gfx_con.fntsz = prevFontSize;
} }
// static int abs(int x)
// {
// if (x < 0)
// return -x;
// return x;
// }
void gfx_set_pixel(u32 x, u32 y, u32 color)
{
gfx_ctxt.fb[x + y * gfx_ctxt.stride] = color;
}
void gfx_line(int x0, int y0, int x1, int y1, u32 color)
{
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = (dx > dy ? dx : -dy) / 2, e2;
while (1)
{
gfx_set_pixel(x0, y0, color);
if (x0 == x1 && y0 == y1)
break;
e2 = err;
if (e2 >-dx)
{
err -= dy;
x0 += sx;
}
if (e2 < dy)
{
err += dx;
y0 += sy;
}
}
}
void gfx_set_rect_grey(const u8 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y)
{
u32 pos = 0;
for (u32 y = pos_y; y < (pos_y + size_y); y++)
{
for (u32 x = pos_x; x < (pos_x + size_x); x++)
{
memset(&gfx_ctxt.fb[x + y*gfx_ctxt.stride], buf[pos], 4);
pos++;
}
}
}
void gfx_set_rect_rgb(const u8 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y)
{
u32 pos = 0;
for (u32 y = pos_y; y < (pos_y + size_y); y++)
{
for (u32 x = pos_x; x < (pos_x + size_x); x++)
{
gfx_ctxt.fb[x + y * gfx_ctxt.stride] = buf[pos + 2] | (buf[pos + 1] << 8) | (buf[pos] << 16);
pos+=3;
}
}
}
void gfx_set_rect_argb(const u32 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y)
{
u32 *ptr = (u32 *)buf;
for (u32 y = pos_y; y < (pos_y + size_y); y++)
for (u32 x = pos_x; x < (pos_x + size_x); x++)
gfx_ctxt.fb[x + y * gfx_ctxt.stride] = *ptr++;
}
void gfx_render_bmp_argb(const u32 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y)
{
for (u32 y = pos_y; y < (pos_y + size_y); y++)
{
for (u32 x = pos_x; x < (pos_x + size_x); x++)
gfx_ctxt.fb[x + y * gfx_ctxt.stride] = buf[(size_y + pos_y - 1 - y ) * size_x + x - pos_x];
}
}
u8 *gfx_bmp_screenshot(u32 *size) u8 *gfx_bmp_screenshot(u32 *size)
{ {
// 14 bytes // 14 bytes
@ -627,3 +547,78 @@ u8 *gfx_bmp_screenshot(u32 *size)
*size = file_size; *size = file_size;
return buffer; return buffer;
} }
#endif
void gfx_set_pixel(u32 x, u32 y, u32 color)
{
gfx_ctxt.fb[x + y * gfx_ctxt.stride] = color;
}
void gfx_line(int x0, int y0, int x1, int y1, u32 color)
{
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = (dx > dy ? dx : -dy) / 2, e2;
while (1)
{
gfx_set_pixel(x0, y0, color);
if (x0 == x1 && y0 == y1)
break;
e2 = err;
if (e2 >-dx)
{
err -= dy;
x0 += sx;
}
if (e2 < dy)
{
err += dx;
y0 += sy;
}
}
}
void gfx_set_rect_grey(const u8 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y)
{
u32 pos = 0;
for (u32 y = pos_y; y < (pos_y + size_y); y++)
{
for (u32 x = pos_x; x < (pos_x + size_x); x++)
{
memset(&gfx_ctxt.fb[x + y*gfx_ctxt.stride], buf[pos], 4);
pos++;
}
}
}
void gfx_set_rect_rgb(const u8 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y)
{
u32 pos = 0;
for (u32 y = pos_y; y < (pos_y + size_y); y++)
{
for (u32 x = pos_x; x < (pos_x + size_x); x++)
{
gfx_ctxt.fb[x + y * gfx_ctxt.stride] = buf[pos + 2] | (buf[pos + 1] << 8) | (buf[pos] << 16);
pos+=3;
}
}
}
void gfx_set_rect_argb(const u32 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y)
{
u32 *ptr = (u32 *)buf;
for (u32 y = pos_y; y < (pos_y + size_y); y++)
for (u32 x = pos_x; x < (pos_x + size_x); x++)
gfx_ctxt.fb[x + y * gfx_ctxt.stride] = *ptr++;
}
void gfx_render_bmp_argb(const u32 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y)
{
for (u32 y = pos_y; y < (pos_y + size_y); y++)
{
for (u32 x = pos_x; x < (pos_x + size_x); x++)
gfx_ctxt.fb[x + y * gfx_ctxt.stride] = buf[(size_y + pos_y - 1 - y ) * size_x + x - pos_x];
}
}

View file

@ -38,8 +38,10 @@ void gfx_putc(char c);
void gfx_puts(const char *s); void gfx_puts(const char *s);
void gfx_printf(const char *fmt, ...); void gfx_printf(const char *fmt, ...);
void gfx_print_header(); void gfx_print_header();
#ifdef DEBUG
void gfx_hexdump(u32 base, const u8 *buf, u32 len); void gfx_hexdump(u32 base, const u8 *buf, u32 len);
u8 *gfx_bmp_screenshot(u32 *size);
#endif
void gfx_set_pixel(u32 x, u32 y, u32 color); void gfx_set_pixel(u32 x, u32 y, u32 color);
void gfx_line(int x0, int y0, int x1, int y1, u32 color); void gfx_line(int x0, int y0, int x1, int y1, u32 color);
void gfx_put_small_sep(); void gfx_put_small_sep();
@ -48,7 +50,6 @@ void gfx_set_rect_grey(const u8 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos
void gfx_set_rect_rgb(const u8 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y); void gfx_set_rect_rgb(const u8 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y);
void gfx_set_rect_argb(const u32 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y); void gfx_set_rect_argb(const u32 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y);
void gfx_render_bmp_argb(const u32 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y); void gfx_render_bmp_argb(const u32 *buf, u32 size_x, u32 size_y, u32 pos_x, u32 pos_y);
u8 *gfx_bmp_screenshot(u32 *size);
// Global gfx console and context. // Global gfx console and context.
gfx_ctxt_t gfx_ctxt; gfx_ctxt_t gfx_ctxt;

View file

@ -45,6 +45,20 @@
#include "io/io.h" #include "io/io.h"
#include <string.h> #include <string.h>
#define RETRY_COUNT 5
#define RETRY(exp) \
({ \
u8 _attemptc_ = RETRY_COUNT; \
bool _resultb_; \
while (_attemptc_--) \
{ \
if ((_resultb_ = exp)) \
break; \
gfx_printf("%kretry %d/%d...\n", COLOR_RED, RETRY_COUNT - _attemptc_, RETRY_COUNT); \
} \
_resultb_; \
})
extern bool sd_mount(); extern bool sd_mount();
extern void sd_unmount(); extern void sd_unmount();
extern int sd_save_to_file(void *buf, u32 size, const char *filename); extern int sd_save_to_file(void *buf, u32 size, const char *filename);
@ -208,8 +222,8 @@ bool dump_keys()
if (memcmp(keyblob_block, keyblob_mac, 0x10)) if (memcmp(keyblob_block, keyblob_mac, 0x10))
{ {
EPRINTFARGS("Keyblob %x corrupt.", i); EPRINTFARGS("Keyblob %x corrupt.", i);
gfx_hexdump(i, keyblob_block, 0x10); // gfx_hexdump(i, keyblob_block, 0x10);
gfx_hexdump(i, keyblob_mac, 0x10); // gfx_hexdump(i, keyblob_mac, 0x10);
continue; continue;
} }
@ -423,7 +437,7 @@ bool readData(u8 *buffer, u32 offset, u32 length, void (*progress_callback)(u32,
while (clusterOffset + sectorCount > SECTORS_IN_CLUSTER) while (clusterOffset + sectorCount > SECTORS_IN_CLUSTER)
{ {
u32 sectorsToRead = SECTORS_IN_CLUSTER - clusterOffset; u32 sectorsToRead = SECTORS_IN_CLUSTER - clusterOffset;
if (!prodinfo_read(tmp + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorsToRead)) if (!RETRY(prodinfo_read(tmp + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorsToRead)))
goto out; goto out;
sector += sectorsToRead; sector += sectorsToRead;
@ -438,7 +452,7 @@ bool readData(u8 *buffer, u32 offset, u32 length, void (*progress_callback)(u32,
if (sectorCount == 0) if (sectorCount == 0)
goto done; goto done;
if (!prodinfo_read(tmp + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorCount)) if (!RETRY(prodinfo_read(tmp + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorCount)))
goto out; goto out;
memcpy(buffer, tmp + newOffset, length); memcpy(buffer, tmp + newOffset, length);
@ -482,11 +496,11 @@ bool writeData(u8 *buffer, u32 offset, u32 length, void (*progress_callback)(u32
{ {
bytesToWrite = length; bytesToWrite = length;
} }
if (!prodinfo_read(tmp_sec, sector, 1)) if (!RETRY(prodinfo_read(tmp_sec, sector, 1)))
goto out; goto out;
memcpy(tmp_sec + newOffset, buffer, bytesToWrite); memcpy(tmp_sec + newOffset, buffer, bytesToWrite);
if (!prodinfo_write(tmp_sec, sector, 1)) if (!RETRY(prodinfo_write(tmp_sec, sector, 1)))
goto out; goto out;
sector++; sector++;
@ -511,7 +525,7 @@ bool writeData(u8 *buffer, u32 offset, u32 length, void (*progress_callback)(u32
while (clusterOffset + sectorCount >= SECTORS_IN_CLUSTER) while (clusterOffset + sectorCount >= SECTORS_IN_CLUSTER)
{ {
u32 sectorsToRead = SECTORS_IN_CLUSTER - clusterOffset; u32 sectorsToRead = SECTORS_IN_CLUSTER - clusterOffset;
if (!prodinfo_write(buffer + newOffset + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorsToRead)) if (!RETRY(prodinfo_write(buffer + newOffset + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorsToRead)))
goto out; goto out;
sector += sectorsToRead; sector += sectorsToRead;
@ -529,7 +543,7 @@ bool writeData(u8 *buffer, u32 offset, u32 length, void (*progress_callback)(u32
// write remaining sectors // write remaining sectors
if (sectorCount > 0) if (sectorCount > 0)
{ {
if (!prodinfo_write(buffer + newOffset + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorCount)) if (!RETRY(prodinfo_write(buffer + newOffset + (sectorOffset * NX_EMMC_BLOCKSIZE), sector, sectorCount)))
goto out; goto out;
length -= sectorCount * NX_EMMC_BLOCKSIZE; length -= sectorCount * NX_EMMC_BLOCKSIZE;
@ -552,11 +566,11 @@ bool writeData(u8 *buffer, u32 offset, u32 length, void (*progress_callback)(u32
goto out; goto out;
} }
if (!prodinfo_read(tmp_sec, sector, 1)) if (!RETRY(prodinfo_read(tmp_sec, sector, 1)))
goto out; goto out;
memcpy(tmp_sec, buffer + newOffset + (sectorOffset * NX_EMMC_BLOCKSIZE), length); memcpy(tmp_sec, buffer + newOffset + (sectorOffset * NX_EMMC_BLOCKSIZE), length);
if (!prodinfo_write(tmp_sec, sector, 1)) if (!RETRY(prodinfo_write(tmp_sec, sector, 1)))
goto out; goto out;
done: done:
@ -592,6 +606,7 @@ out:
return result; return result;
} }
#ifdef DEBUG
void screenshot(const char *filename) void screenshot(const char *filename)
{ {
sd_mount(); sd_mount();
@ -609,6 +624,7 @@ void screenshot(const char *filename)
f_close(&fp); f_close(&fp);
free(buffer); free(buffer);
} }
#endif
bool verifyHash(u32 hashOffset, u32 offset, u32 sz) bool verifyHash(u32 hashOffset, u32 offset, u32 sz)
{ {
@ -627,8 +643,8 @@ bool verifyHash(u32 hashOffset, u32 offset, u32 sz)
if (memcmp(hash1, hash2, 0x20)) if (memcmp(hash1, hash2, 0x20))
{ {
EPRINTF("error: hash verification failed\n"); EPRINTF("error: hash verification failed\n");
gfx_hexdump(0, hash1, 0x20); // gfx_hexdump(0, hash1, 0x20);
gfx_hexdump(0, hash2, 0x20); // gfx_hexdump(0, hash2, 0x20);
goto out; goto out;
} }

View file

@ -84,7 +84,7 @@ bool prodinfo_read(
u32 count /* Number of sectors to read */ u32 count /* Number of sectors to read */
) )
{ {
bool result = false;
__attribute__((aligned(16))) static u8 tweak[0x10]; __attribute__((aligned(16))) static u8 tweak[0x10];
__attribute__((aligned(16))) static u64 prev_cluster = -1; __attribute__((aligned(16))) static u64 prev_cluster = -1;
__attribute__((aligned(16))) static u32 prev_sector = 0; __attribute__((aligned(16))) static u32 prev_sector = 0;
@ -109,13 +109,13 @@ bool prodinfo_read(
} }
// fatfs will never pull more than a cluster // fatfs will never pull more than a cluster
_emmc_xts(9, 8, 0, tweak, regen_tweak, tweak_exp, prev_cluster, buff, buff, count * 0x200); result = _emmc_xts(9, 8, 0, tweak, regen_tweak, tweak_exp, prev_cluster, buff, buff, count * 0x200);
prev_sector = sector + count - 1; prev_sector = sector + count - 1;
return true; return result;
} }
return false; return result;
} }
bool prodinfo_write( bool prodinfo_write(
@ -124,7 +124,6 @@ bool prodinfo_write(
u32 count /* Number of sectors to read */ u32 count /* Number of sectors to read */
) )
{ {
__attribute__((aligned(16))) static u8 tweak[0x10]; __attribute__((aligned(16))) static u8 tweak[0x10];
__attribute__((aligned(16))) static u64 prev_cluster = -1; __attribute__((aligned(16))) static u64 prev_cluster = -1;
__attribute__((aligned(16))) static u32 prev_sector = 0; __attribute__((aligned(16))) static u32 prev_sector = 0;
@ -147,7 +146,9 @@ bool prodinfo_write(
} }
// fatfs will never pull more than a cluster // fatfs will never pull more than a cluster
_emmc_xts(9, 8, 1, tweak, regen_tweak, tweak_exp, prev_cluster, buff, buff, count * 0x200); if(!_emmc_xts(9, 8, 1, tweak, regen_tweak, tweak_exp, prev_cluster, buff, buff, count * 0x200)){
return false;
}
if (nx_emmc_part_write(&storage, prodinfo_part, sector, count, buff)) if (nx_emmc_part_write(&storage, prodinfo_part, sector, count, buff))
{ {
prev_sector = sector + count - 1; prev_sector = sector + count - 1;