Skip to content

Commit 7c8e171

Browse files
committed
tried to override the keyboard task (doesn't boot)
1 parent 97f1284 commit 7c8e171

File tree

7 files changed

+127
-9
lines changed

7 files changed

+127
-9
lines changed

platform/m6/kbd.c

+103-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@
77

88
#include "kbd.h"
99

10+
static long kbd_status = 0;
11+
static long kbd_mod_prev_1 = 0xffffffff;
12+
13+
ssymkeys symkeys[] = {
14+
{ 0x00020000, 0, 0x9a1, 0x9a2 }, // KEY_SHOOT_HALF
15+
{ 0x00040000, 0, 0x9a3, 0x9a4 }, // KEY_SHOOT_FULL_ONLY
16+
{ 0, 0, 0, 0 } };
17+
1018
// use by kbd_common
1119
long kbd_new_state[3] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
1220
long kbd_prev_state[3] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
1321
long kbd_mod_state[3] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
1422

15-
KeyMap keymap[] = {
16-
{ 0, KEY_PLAYBACK, 0x00000002 }, // Found @0xe05e4cdc, levent 0x101 (uses inverted logic in physw_status)
23+
KeyMap keymap[] = { { 0, KEY_PLAYBACK, 0x00000002 }, // Found @0xe05e4cdc, levent 0x101 (uses inverted logic in physw_status)
1724
{ 0, KEY_WIFI, 0x00000004 }, // Found @0xe05e4ce4, levent 0x103 (uses inverted logic in physw_status)
1825
{ 1, KEY_MENU, 0x00000080 }, // Found @0xe05e4d1c, levent 0x15
1926
{ 1, KEY_UP, 0x00000100 }, // Found @0xe05e4d24, levent 0x06
@@ -23,8 +30,7 @@ KeyMap keymap[] = {
2330
{ 1, KEY_SET, 0x00001000 }, // Found @0xe05e4d44, levent 0x0a
2431
{ 1, KEY_ERASE, 0x00008000 }, // Found @0xe05e4d5c, levent 0x0b
2532
{ 2, KEY_VIDEO, 0x00002000 }, // Found @0xe05e4db4, levent 0x02
26-
{ 0, 0, 0 }
27-
};
33+
{ 0, 0, 0 } };
2834

2935
/**
3036
* @header usb_remote.h
@@ -43,3 +49,96 @@ void kbd_fetch_data(long *dst) {
4349
_GetKbdState(dst);
4450
_kbd_read_keys_r2(dst);
4551
}
52+
53+
void my_kbd_read_keys() {
54+
kbd_prev_state[0] = kbd_new_state[0];
55+
kbd_prev_state[1] = kbd_new_state[1];
56+
kbd_prev_state[2] = kbd_new_state[2];
57+
58+
// note assumed kbd_pwr_on has been called if needed
59+
kbd_fetch_data(physw_status); // has to be physw_status
60+
61+
kbd_new_state[0] = physw_status[0] ^ KEYS_INV0; // invert button(s) for CHDK use
62+
kbd_new_state[1] = physw_status[1];
63+
kbd_new_state[2] = physw_status[2];
64+
65+
static long kbd_status_prev = 0;
66+
kbd_status = kbd_process();
67+
68+
if (kbd_status == 0) {
69+
// handle sw1 and sw2
70+
if (kbd_status_prev != 0) {
71+
int n = 0;
72+
while (symkeys[n].bit) {
73+
if (symkeys[n].chdkstate) {
74+
PostLogicalEventToUI(symkeys[n].unpresse, 0);
75+
symkeys[n].chdkstate = 0;
76+
}
77+
n++;
78+
}
79+
_post_msg_3_to_physw_releasesw();
80+
}
81+
82+
} else { // override keys
83+
// invert button(s) before writing back to physw_status
84+
physw_status[0] = ((kbd_new_state[0] & (~KEYS_MASK0))
85+
| (kbd_mod_state[0] & KEYS_MASK0)) ^ KEYS_INV0;
86+
87+
physw_status[1] = (kbd_new_state[1] & (~KEYS_MASK1))
88+
| (kbd_mod_state[1] & KEYS_MASK1);
89+
90+
physw_status[2] = (kbd_new_state[2] & (~KEYS_MASK2))
91+
| (kbd_mod_state[2] & KEYS_MASK2);
92+
93+
// handle sw1 and sw2 from kbd_mod_state, emit events
94+
// TODO: event order won't necessarily match a real hw sequence (shoot_half might be released sooner than shoot_full and so on)
95+
int n = 0;
96+
while (symkeys[n].bit) {
97+
long bit = symkeys[n].bit;
98+
if ((kbd_mod_state[1] & bit) < (kbd_mod_prev_1 & bit)) {
99+
PostLogicalEventToUI(symkeys[n].presse, 0);
100+
symkeys[n].chdkstate = 1;
101+
} else if ((kbd_mod_state[1] & bit) > (kbd_mod_prev_1 & bit)) {
102+
PostLogicalEventToUI(symkeys[n].unpresse, 0);
103+
symkeys[n].chdkstate = 0;
104+
}
105+
n++;
106+
}
107+
kbd_mod_prev_1 = kbd_mod_state[1];
108+
}
109+
kbd_status_prev = kbd_status;
110+
111+
// USB and SD read-only are standard
112+
kbd_update_physw_bits();
113+
}
114+
115+
long __attribute__((naked,noinline)) wrap_kbd_p1_f() {
116+
asm volatile(
117+
"push {r1-r7, lr}\n"
118+
"movs r4, #0\n"
119+
"bl my_kbd_read_keys\n"
120+
"b _kbd_p1_f_cont\n"
121+
);
122+
123+
return 0;
124+
}
125+
126+
/**
127+
* @header lolevel.h
128+
*/
129+
void __attribute__((naked,noinline)) mykbd_task() {
130+
// initialize our own kbd_new_state[] array with the current physical status
131+
kbd_new_state[0] = physw_status[0] ^ KEYS_INV0;
132+
kbd_new_state[1] = physw_status[1];
133+
kbd_new_state[2] = physw_status[2];
134+
135+
while (physw_run) {
136+
_SleepTask(physw_sleep_delay);
137+
138+
if (wrap_kbd_p1_f() == 1) {
139+
_kbd_p2_f();
140+
}
141+
}
142+
143+
_ExitTask();
144+
}

platform/m6/kbd.h

+8
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@
22
#define M6_KBD_H
33

44
extern void _GetKbdState(long*);
5+
extern void _post_msg_3_to_physw_releasesw(void);
6+
7+
typedef struct {
8+
long bit; // bitmask in physw_status
9+
unsigned chdkstate; // nonzero if pressed
10+
unsigned presse; // press levent
11+
unsigned unpresse; // unpress levent
12+
} ssymkeys;
513

614
#endif

platform/m6/makefile.inc

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ ROMBASEADDR=0xE0000000
1111

1212
override OPT_FI2=
1313

14-
# TODO: check
1514
MAXRAMADDR = 0x3fffffff
1615

17-
# TODO: check
1816
NEED_ENCODED_DISKBOOT=19
19-
KEYSYS=d4k
17+
KEYSYS=d4k // TODO
2018
FI2FLAGS=W
2119

2220
# next line is for the compatibility check, as comment

platform/m6/platform_kbd.h

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define HOTSHOE_IDX 2
1818

1919
// handwrittten
20+
#define KEYS_INV0 (0x00000006)
2021
#define KEYS_MASK0 (0x00000006) // TODO: check
2122
#define KEYS_MASK1 (0x00063f80) // TODO: check
2223
#define KEYS_MASK2 (0x00000000) // TODO: check

platform/m6/sub/101a/boot.c

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "core.h"
3+
#include "lolevel.h"
34

45
#include "../../../generic/mmu_utils.h"
56
#include "boot.h"
@@ -53,11 +54,22 @@ void __attribute__((naked,noinline,aligned(4))) patch_E0079DA4() {
5354
);
5455
}
5556

57+
void __attribute__((naked,noinline,aligned(4))) patch_E007A1E8() {
58+
asm volatile (
59+
"ldr r3, =mykbd_task\n"
60+
"movs r0, #0\n"
61+
"mov r2, #0x2000\n"
62+
"strd r0, r1, [sp]\n"
63+
"ldr pc, =0xe007a1f1\n" // -> back to ROM
64+
);
65+
}
66+
5667
void plant_hacks_for_e0070000(unsigned addr) {
5768
// insert startup mode handling
5869
place_fw_patch_t2_64b(patch_E0079DA4, addr + 0x9DA4);
70+
place_fw_patch_t2_64b(patch_E007A1E8, addr + 0xA1E8);
5971
// nop (benign 16-bit instruction, added to replace 1st half of destroyed 32-bit instruction)
60-
*(unsigned short*) (addr + 0x9DA2) = 0xbf00;
72+
*(unsigned short*) (addr + 0xA1E6) = 0xbf00;
6173
}
6274

6375
// should be integrated in dcache_clean_all

platform/m6/sub/101a/boot.h

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
extern void _set_canon_mmu_tables_to(unsigned addr);
55
extern void _make_copy_of_canon_mmutables(unsigned dest_addr);
6-
extern int _CreateTask(char *name, int prio, int stack_size, void *entry, int parm);
76
extern int _PostLogicalEventToUI(int event, int unk);
87
extern void task_InitFileModules(void);
98

platform/m6/sub/101a/stubs_entry_2.S

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ NHSTUB(reboot_fw_update, 0xe039b733)
2121
NHSTUB(GetImageFolder, 0xe02e3031)
2222
NHSTUB(WriteSDCard, 0xe04faf19)
2323
NHSTUB(MakeSDCardBootable, 0xe036111b)
24+
NHSTUB(post_msg_3_to_physw_releasesw, 0xe0070a3b)
2425

2526
IGNORE(EngDrvRead)
2627
NHSTUB(EngDrvRead_FW, 0xe05b1d13) // in wrappers

0 commit comments

Comments
 (0)