Skip to content

Commit bf2d233

Browse files
Initial commit
1 parent 8119277 commit bf2d233

32 files changed

Lines changed: 4214 additions & 0 deletions

LeafSDTools/Display.cpp

Lines changed: 699 additions & 0 deletions
Large diffs are not rendered by default.

LeafSDTools/Display.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#ifndef DISPLAY_H
2+
#define DISPLAY_H
3+
4+
// Common
5+
6+
#define COLOR_WHITE 0xFFFF // RGB(255,255,255)
7+
#define COLOR_LIGHT_GRAY 0xC618 // RGB(192,192,192)
8+
#define COLOR_DARK_GRAY 0x630C // RGB(128,128,128)
9+
#define COLOR_BLACK 0x0000 // RGB(0,0,0)
10+
11+
// Debug parameters
12+
#define LOG_PIXELS 0
13+
#define LOG_SURFACES 0
14+
// Function pointers
15+
16+
17+
typedef struct {
18+
DWORD size; // Size of the structure (0x34)
19+
DWORD reserved2; // Zero-initialized reserved fields (7 fields)
20+
DWORD reserved3; // Zero-initialized reserved fields (7 fields)
21+
DWORD width; // Width of the surface (800)
22+
DWORD height; // Height of the surface (800)
23+
DWORD reserved1; // Zero-initialized reserved fields (7 fields)
24+
DWORD reserved5; // Zero-initialized reserved fields (7 fields)
25+
DWORD width2; // Possibly redundant width or stride (0x1e0 = 480)
26+
DWORD height2; // Possibly redundant height or format-related (0x1e0 = 480)
27+
DWORD reserved4; // Zero-initialized reserved fields (7 fields)
28+
DWORD max_level; // Maximum level or depth (0xff = 255)
29+
DWORD format; // Surface format (7, likely an enum or flag)
30+
DWORD flags; // Configuration flags (0x4000)
31+
} GdisRenderContext;
32+
33+
34+
/*
35+
36+
0, 0x08, 8: 0x3c
37+
4, 0x0c, 12: 0x1e0
38+
8, 0x10, 16: 800
39+
12, 0x14, 20: 0x353635
40+
16, 0x18, 24: 0x10
41+
20, 0x1c, 28: local_18 * 10 + 0x10
42+
24, 0x20, 32: 0x81
43+
32, 0x28, 40: bffr
44+
40, 0x30, 48: len
45+
46+
*/
47+
typedef struct {
48+
DWORD size; // 0, 0x3c
49+
DWORD height; // 4, 0x1e0 (480)
50+
DWORD width; // 8, 800
51+
DWORD pixelFormat; // 12, 0x353635 (RGB565)
52+
DWORD unknown1; // 16, 0x10
53+
DWORD unknown2; // 20, 0x10 or 0x1a
54+
DWORD flags; // 24, 0x81
55+
DWORD unknown3; // 28
56+
DWORD buffer; // 32, 0x640
57+
DWORD unknown4; // 36
58+
DWORD bufferSize; // 40, e.g., 0x56000000
59+
DWORD reserved2c; // 0x2c: Reserved (0)
60+
DWORD reserved30; // 0x30: Reserved (0)
61+
DWORD reserved34; // 0x34: Reserved (0)
62+
DWORD reserved38; // 0x38: Reserved (0)
63+
} GdisSurface;
64+
65+
66+
// Functions
67+
int SetupRenderContext();
68+
int LockSurface(DWORD surface, DWORD* output);
69+
int LockSurfaceAlt(DWORD surface, DWORD* output);
70+
int UnlockSurface(DWORD surface);
71+
int SwapBuffers();
72+
int InitGraphic();
73+
int DestroySurfaces();
74+
int ScrollScreen();
75+
void AdjustTextPosition(int x, int y);
76+
void AdjustBoundaries(int x);
77+
int RenderChar(byte c, int x, int y, WORD color, int scale);
78+
int RenderStringWithScale(const byte* str, int scale);
79+
int RenderString(const byte* str);
80+
void PrintScreenWithScale(const byte* str, int scale);
81+
void PrintScreen(const byte* str);
82+
int DrawBackground(DWORD color);
83+
void PrintToScreen(int scale, const char* format, ...);
84+
void DrawRect(int x, int y, int w, int h, WORD color, WORD* pixels);
85+
void DrawChar(byte c, int x, int y, WORD color, int scale, WORD* pixels);
86+
int ClearArea(int x, int y, int w, int h);
87+
int RenderBtnText(int x, int y, const char* text, WORD color, int scale, WORD* pixels);
88+
int RenderButton(int x_right, int y_top, int w, int h, const char* text);
89+
int RenderButtonWithState(int x_right, int y_top, int w, int h, const char* text, bool state);
90+
void ResetTextRenderer();
91+
92+
93+
94+
#endif // DISPLAY_H

LeafSDTools/Flash.cpp

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
#include "stdafx.h"
2+
#include "Flash.h"
3+
4+
5+
/*
6+
7+
SD Card handling functions
8+
9+
*/
10+
11+
int ChangeSDLock(LPCWSTR disk, BYTE* pin, int command) {
12+
if (pin == NULL)
13+
return false;
14+
int result = 0xbad32;
15+
16+
17+
HANDLE hDevice = CreateFileW(disk,GENERIC_READ,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
18+
if (hDevice != INVALID_HANDLE_VALUE) {
19+
DWORD bytesReturned = 0;
20+
DWORD ioctlCode = (command == 0) ? 0x80032020 : 0x80032014;
21+
22+
result = DeviceIoControl(hDevice, ioctlCode, pin, 0x10, NULL, 0, &bytesReturned, NULL);
23+
24+
CloseHandle(hDevice);
25+
}
26+
27+
return result;
28+
}
29+
30+
int CheckMountStatus(WORD command) {
31+
HANDLE hDevice = INVALID_HANDLE_VALUE;
32+
BOOL success = FALSE;
33+
DWORD result = 0xFFFFFFFF;
34+
DWORD output = 0;
35+
DWORD bytesReturned = 0;
36+
37+
// Open device handle for SDC1: (SDC = SD Controller?)
38+
hDevice = CreateFileW(
39+
L"SDC1:", // Device name
40+
GENERIC_READ | GENERIC_WRITE, // Read/write access
41+
0, // No sharing
42+
NULL, // No security attributes
43+
OPEN_EXISTING, // Open existing device
44+
0, // No special attributes
45+
NULL // No template file
46+
);
47+
48+
if (hDevice == INVALID_HANDLE_VALUE) {
49+
LogError(L"CheckMountStatus invalid handle", command);
50+
return result; // Return 0xFFFFFFFF on failure
51+
}
52+
53+
// Send IOCTL command
54+
success = DeviceIoControl(
55+
hDevice, // Device handle
56+
0x80032004, // Custom IOCTL code
57+
&command, // Input buffer (16-bit command)
58+
sizeof(command), // Input buffer size
59+
&output, // Output buffer
60+
sizeof(output), // Output buffer size
61+
&bytesReturned, // Bytes returned
62+
NULL // No overlapped I/O
63+
);
64+
65+
LogError(L"CheckMountStatus", output);
66+
67+
if (success) {
68+
// Process output: return inverted LSB
69+
result = (~output & 1);
70+
}
71+
72+
LogError(L"CheckMountStatus result", result);
73+
74+
// Clean up
75+
CloseHandle(hDevice);
76+
return result;
77+
}
78+
79+
/*
80+
This function signals the SD Card controller to initialise SD card slots.
81+
After inserting/removing a card, it is necessary to issue this command.
82+
*/
83+
void InitSDCards()
84+
{
85+
HANDLE hDevice = CreateFileW(L"SDC1:",
86+
GENERIC_READ | GENERIC_WRITE,
87+
0,
88+
NULL,
89+
OPEN_EXISTING,
90+
0,
91+
NULL);
92+
93+
if (hDevice != INVALID_HANDLE_VALUE) {
94+
short index = 0;
95+
for (int i = 0; i < 2; ++i)
96+
{
97+
BOOL result = DeviceIoControl(hDevice,
98+
0x80032004,
99+
&index,
100+
sizeof(index),
101+
NULL,
102+
0,
103+
NULL,
104+
NULL);
105+
if (!result)
106+
break;
107+
108+
index++;
109+
}
110+
111+
BYTE cmd = 0x11;
112+
DeviceIoControl(hDevice,
113+
0x80032000,
114+
&cmd,
115+
sizeof(cmd),
116+
NULL,
117+
0,
118+
NULL,
119+
NULL);
120+
121+
CloseHandle(hDevice);
122+
}
123+
}
124+
125+
void SDController_CMD17() {
126+
HANDLE hDevice = CreateFileW(L"SDC1:",
127+
GENERIC_READ | GENERIC_WRITE,
128+
0,
129+
NULL,
130+
OPEN_EXISTING,
131+
0,
132+
NULL);
133+
134+
if (hDevice != INVALID_HANDLE_VALUE) {
135+
136+
BYTE cmd = 0x11;
137+
BOOL cmdResult = DeviceIoControl(hDevice,
138+
0x80032000,
139+
&cmd,
140+
sizeof(cmd),
141+
NULL,
142+
0,
143+
NULL,
144+
NULL);
145+
146+
LogError(L"SDController_CMD0x11 result", cmdResult);
147+
CloseHandle(hDevice);
148+
} else {
149+
LogError(L"SDController_CMD0x11, SDC1 handle is invalid", (long)hDevice);
150+
}
151+
}
152+
153+
154+
155+
/*
156+
157+
NAND handling functions
158+
159+
*/
160+
161+
int ReadSingleFlashBlock(int block, DWORD size, BYTE* output) {
162+
LogError(L"ReadSingleFlashBLK: Opening flash block", block);
163+
LogError(L"ReadSingleFlashBLK: Reading size:", size);
164+
HANDLE hFMD1 = CreateFileW(L"FMD1:",0xc0000000,0,0,0,0,0);
165+
if ( hFMD1 != INVALID_HANDLE_VALUE ) {
166+
DWORD param[2] = { 5, 0 };
167+
LogBufferContents(L"BFR: ", param, 4);
168+
param[0] = block;
169+
LogBufferContents(L"AFTR: ", param, 4);
170+
DWORD nRet = 0;
171+
if (DeviceIoControl(hFMD1,0x80112000, param, 8, &output, size, &nRet, 0)) {
172+
if ( nRet == size ) {
173+
CloseHandle(hFMD1);
174+
return 0;
175+
}
176+
return 3;
177+
}
178+
return 2;
179+
}
180+
return 1;
181+
}
182+
183+
BOOL WriteProdDataToFile(int block, BYTE* prodData, DWORD dataSize) {
184+
WCHAR filePath[128];
185+
wsprintf(filePath, L"\\SystemSD\\%03dprod.bin", block);
186+
187+
// Open or create the file
188+
HANDLE hFile = CreateFile(
189+
filePath,
190+
GENERIC_WRITE,
191+
0,
192+
NULL,
193+
CREATE_ALWAYS,
194+
FILE_ATTRIBUTE_NORMAL,
195+
NULL
196+
);
197+
198+
if (hFile == INVALID_HANDLE_VALUE) {
199+
DWORD error = GetLastError();
200+
LogError(L"Failed to open prod.bin, Error code:", error);
201+
return FALSE;
202+
}
203+
204+
// Write prodData to the file
205+
DWORD bytesWritten = 0;
206+
BOOL success = WriteFile(
207+
hFile,
208+
prodData,
209+
dataSize,
210+
&bytesWritten,
211+
NULL
212+
);
213+
214+
if (!success || bytesWritten != dataSize) {
215+
// Handle write error
216+
DWORD error = GetLastError();
217+
LogError(L"Failed to write prodbin. Error code: %d, Bytes written: %d\n",
218+
error);
219+
LogError(L"Bytes written:", bytesWritten);
220+
CloseHandle(hFile);
221+
return FALSE;
222+
}
223+
224+
CloseHandle(hFile);
225+
226+
LogError(L"Saved prodbin to \SystemSD\??prod.bin", 0);
227+
return TRUE;
228+
}
229+
230+
int GetProdSection(CHAR* modelName, BYTE* productId, BYTE* serial, BYTE* pin) {
231+
if (modelName == NULL && serial == NULL && productId == NULL && pin == NULL) {
232+
LogError(L"GetProd: invalid args!", 3);
233+
return 3;
234+
}
235+
236+
BYTE prodData[0x2D1];
237+
int flashBlockReadResult = ReadSingleFlashBlock(5, 0x2D1, (BYTE*)prodData);
238+
if (flashBlockReadResult != 0) {
239+
LogError(L"GetProd: Reading PROG from flash failed", flashBlockReadResult);
240+
return 1;
241+
}
242+
243+
if (productId != NULL)
244+
memcpy(productId, ((BYTE*)prodData) + 0x18, 4);
245+
246+
if (modelName != NULL)
247+
memcpy(modelName, ((BYTE*)prodData) + 0x38, 8);
248+
249+
if (serial != NULL)
250+
memcpy(serial, ((BYTE*)prodData) + 0x48, 4);
251+
252+
if (pin != NULL)
253+
memcpy(pin, ((BYTE*)prodData) + 0x58, 4);
254+
255+
return 0;
256+
}
257+
258+
int ReadFullSDPin(BYTE* pin) {
259+
if (pin == NULL) {
260+
LogError(L"ReadFullPin: invalid arg!", 3);
261+
return 3;
262+
}
263+
BYTE prodData[0x2D1];
264+
int flashBlockReadResult = ReadSingleFlashBlock(5, 0x2D1, (BYTE*)prodData);
265+
if (flashBlockReadResult != 0) {
266+
LogError(L"GetProd: Reading PROG from flash failed", flashBlockReadResult);
267+
return 1;
268+
}
269+
270+
memcpy(pin, ((BYTE*)prodData) + 0xf8, 0x10);
271+
return 0;
272+
}

LeafSDTools/Flash.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef FLASH_H
2+
#define FLASH_H
3+
4+
#define SD1_DISK L"DSK1:"
5+
#define SD2_DISK L"DSK8:"
6+
7+
int GetProdSection(CHAR* modelName, BYTE* productId, BYTE* serial, BYTE* pin);
8+
int ReadSingleFlashBlock(int block, DWORD size, BYTE* output);
9+
10+
// SD
11+
int ReadFullSDPin(BYTE* pin);
12+
int ChangeSDLock(LPCWSTR disk, BYTE* pin, int command);
13+
int CheckMountStatus(WORD command);
14+
void InitSDCards();
15+
void SDController_CMD17();
16+
17+
#endif

0 commit comments

Comments
 (0)