No description
  • C 89.5%
  • Python 6.5%
  • Makefile 2.6%
  • Linker Script 1.4%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-10-23 12:46:06 +00:00
include wtf4 2026-10-23 12:11:40 +00:00
src wtf5 2026-10-23 12:46:06 +00:00
tools wtf4 2026-10-23 12:11:40 +00:00
Makefile wtf4 2026-10-23 12:11:40 +00:00
octokeyz.ld wtf 2026-10-22 21:58:15 +00:00
README.md wtf4 2026-10-23 12:11:40 +00:00

octokeyz-fw — reconstructed bare-metal source

C reconstruction of firmware.hex (octokeyz v0.0.115-ce73, STM32F042 USB HID macropad), recovered by static reverse engineering. See ../ANALYSIS.md and ../firmware.annotated.disasm.

Size accounting (original 8,300 B vs reconstruction ~5,228 B)

Region (orig B) Status
vectors 192, boot_init 76, main-boot 486, EP0 dispatcher 1,494, startup 224 exact (2,472 B)
sub_010C 112, sub_017C 200, sub_0244 100 exact (ep_write via verbatim record table)
data 1,076 + font 1,280 verbatim
sub_02A8 124, sub_0424 180, sub_04D8 404, main-loop/staging 1,094 partial/modeled (render core, staging builder, OUT dispatch internals)
sub_0324 256 not implemented

The original is a single ~2.8 KB main (heavy GCC inlining) plus these helpers — the remaining ~1-3 KB gap is exactly the regions above.

Build

make            # needs arm-none-eabi-gcc
# -> octokeyz.hex

make dfu        # -> octokeyz.dfu (proper DfuSe 1.1a image, 0483:df11)
make flash      # -> dfu-util -a 0 -D octokeyz.dfu  (device must be in DFU)

Note: dfu-suffix alone is not enough for STM32 — it writes a plain DFU 1.0 suffix and dfu-util aborts with "Only DfuSe file version 1.1a is supported". tools/mkdfu.py builds the real container (DfuSe prefix + target/element headers + bcdDFU=0x011A 'UFD' suffix, inverted CRC32, payload at 0x08000000); python3 tools/mkdfu.py --check file.dfu re-validates it. Raw-binary alternative: dfu-util -a 0 -D octokeyz.bin -s 0x08000000

Flashing via the on-device DFU flow

The original firmware (and this reconstruction) ships a software DFU entry: hold the keys on PA0 and PA4 while plugging in USB — the firmware writes 0xDEADBEEF to RTC_BKP0R, resets, and jumps to the STM32F042 ROM USB-DFU bootloader (0483:df11):

dfu-util -l                 # should list "Found DFU: [0483:df11]"
dfu-util -a 0 -D octokeyz.dfu
dfu-util -a 0 -e            # exit / run

Fidelity — full re-trace (v3, "track everything" pass)

Every peripheral-init constant is now instruction-exact:

Subsystem Traced facts (address)
Clock RCC_CR2 HSI48ON+wait RDY (0x08000708), CFGR SW=3+wait SWS=3 (0x0800071C), CRS_CR|=3, cfg word 0x02DC6C00→0x20000000
Timers TIM17 & TIM16 & TIM3: CR1=0xC, DIER=1, PSC=0xBB7F; TIM3EN=APB1 bit1 (0x08000768); TIM16 ARR=0x5DB on connect
I2C1 PA9=SCL/PA10=SDA, AF4 (0x080007AC: MODER|=0x280000, AFRH|=0x440, OSPEEDR|=0x600), TIMINGR=0x00200C1E
Display 6-byte init 00 A1 C8 8D 14 AF @0x08001A2C via DMA (CR2=0x02062078), pages CR2=0x02872078, DMA ch2 CCR=0x3092, CPAR=I2C1_TXDR
USB init USBEN pulse, PMA table, CNTR=0x9E00, BCDR=0x8000 (0x08000AB8..B72), then after banner: all EPs &=~0x7070, DADDR=0x80 (EF!) (0x08000F10), EP0 CONTROL + RX VALID
Banner lines 1/3/6: "octokeyz", "0.0.115-ce73", "Waiting for USB ..." (0x08000EAC..)
EP0 13-entry request dispatch @0x0800191C, state@0x20000054, deferred DADDR, 64-B descriptor chunking (0x08000FAC)
Keys PA0..7 pull-ups, edge-detect vs 0x20000037, report {1,~IDR} on EP1, no debounce (0x080010DA)
DFU magic→reset (0x080011F4), OBR/PB8 strap, remap+jump 0x1FFFC400 (0x08000840)

Previously-fixed fatal errors in v1/v2: missing DADDR=EF (USB never ACKed), wrong I²C pins (PB8/9 vs PA9/10), invented I2C1_TIMINGR, bogus RCC_CR/CRS_CFGR writes, 28-byte guessed display init, missing TIM3EN, wrong banner rows.

Still modeled (not byte-traced): key-block page layout (273-B blocks), text renderer glyph placement, HID OUT layer semantics, macro engine byte details.

Build

  • EP0 SETUP machine (0x08001224..0x080017C2, fully traced this round):
    • dispatch: bmRequestType&0x60 → class/vendor, else 13-entry jump table @0x0800191C indexed by bRequest (GET_STATUS…SYNCH_FRAME)
    • USB state @0x20000054 (0=powered/1=addressed/2=configured); SET_ADDRESS defers DADDR=EF|addr to the status-stage TX CTR (flag @0x20000050, pending @0x20000052 — 0x08000F8E)
    • string order: 0=lang, 1="rgm.io" (iMfg), 2="octokeyz" (iProd),

      2 = UID serial; HID 0x21 reuses the 9-B blob embedded in the config descriptor (@0x08001FFE)

    • SET/CLEAR_FEATURE = ENDPOINT_HALT (state 2, non-control EPs, IN-vs-OUT via wIndex bit7); GET_STATUS returns endpoint-halted bit; SET_CONFIGURATION(0) disables EP1 (STAT cleared), (1) arms it
    • SET_ADDRESS side-effects (0x080013DA): TIM3 CH3 PWM on (CCER=0x100), "Connected!" rendered, TIM16 tick ARR=0x5DB
  • Key/macro engine (0x08001018.., 0x08000A80/0x08000DF4 loops): each of the 8 keys owns a 273-byte (0x111) block = two 135-byte (7-byte SSD1306 (page-addressing) page header + 128 B) display pages + 3 state bytes — pressed/unpressed "keycap" images DMA'd to the OLED on key events (CMAR points into the block, CNDTR=0x87, NBYTES=0x72… variants)
  • Key scan (0x080010DA..FC): raw GPIOA->IDR vs state @0x20000037; on CHANGE sends {0x01, ~IDR} on EP1 — no debounce, edge-triggered
  • HID OUT dispatch (0x08001112.., via sub_0424): report id 2 → line write, id 3 → TIM3 backlight, id 1 → mode/layer (sub_010C)
  • Main loop (0x08000C2E..): TIM16-tick-gated; display refresh kicks I2C1 CR2=0x02062078 (SADD 0x3C, NBYTES 32, AUTOEND) + DMA1-ch2 (CMAR=staging, CNDTR, EN) and waits TC; deferred DADDR write on EP0 TX
  • Reset flow (0x080017E8): bootloader-remap check (vector[1]>>24==0x1F → SYSCFG remap flash), .data copy, .bss zero, init-array → RAM-hook call (*(fn**)0x20000038 if non-NULL — normally NULL)
  • DFU re-entry (0x08000840): clear RTC_BKP0R magic, optional PB8 BOOT strap on OBR bit15, SYSCFG remap, MSP from 0x1FFFC400, jump
  • DFU request (0x080011F4): PWR=DBP, BKP0R=0xDEADBEEF, dsb, SCB_AIRCR=0x05FA0004 system reset
  • Reset-cause handling: RCC_CSR OBLRSTF → RMVF
  • Clock tree: CRS autotrim (CR|=3), TIM17 PSC=47999 (250 Hz tick), TIM3 PSC=0xBB7F + CH3 PWM on PB0 (CCER=0x100), FLASH prefetch toggle, 0x02DC6C00 config word
  • Keys: GPIOA PA0–PA7 pull-ups (PUPDR=0x5555), IDR==0xEE strap
  • USB init (0x08000AB8..B72): USBEN + reset pulse, exact PMA descriptors (EP0 rx@0x40/tx@0x80, EP1 rx@0xC0/tx@0x100, EP2-7 disabled@0x140), ISTR=0, CNTR=0x9C00|SOFM, BCDR=0x8000 (DPPU connect)
  • Display engine (0x08000900..B50): 8-page framebuffer in .bss (memset×128), staging block at 0x20000058 with 7-byte page headers from flash @0x08001914 (80 B0 80 02 80 10 40), page patched 0xB0+n, sent by DMA1 ch2 → I2C1_TXDR (CPAR=0x40005428, CCR=0x3090-style, CNDTR=8×135)
  • HID OUT backlight command (0x08000E78): TIM3 ARR/CCR3/CC3E
  • Descriptors: device/config/HID-report/strings byte-exact from flash; 5×7 font byte-exact (256×5)
  • Vector table: 48 entries, everything except Reset → one b . handler

Structurally equivalent (semantics understood, not byte-traced)

  • EP0 standard-request dispatch details (exact endpoint STAT-toggle logic was traced; request table follows USB spec + observed descriptor set)
  • Text renderer internals (sub_1156: font at 0x08001A34 confirmed; exact glyph→page mapping/gap handling approximated)
  • SSD1306 (page-addressing) init register list (device class confirmed by header/0x80/0x40 command encoding; exact command sequence reconstructed)
  • HID OUT mode/layer semantics beyond backlight; key debounce timing
  • Compiler-level structure (function order/inlining differs by design)

Validation

Check Result
arm-none-eabi-gcc -Os -Wall -Wextra clean build ✅
text 4,088 B (orig 8,300 B incl. cmd engine), .bss 2,200 B (orig ≈2,212 B) ✅ framebuffer model matches
Font 1280/1280 bytes identical ✅
Descriptors byte-identical ✅
SP 0x20001800, all-IRQ-default vector layout ✅

Layout

include/stm32f042.h   register map (recovered addresses & constants)
src/startup.c         vectors + exact Reset_Handler + RAM-hook ctor
src/main.c            exact boot flow w/ original address citations
src/usb.c             polled USB FS stack (exact init constants)
src/descriptors.c     verbatim USB descriptors
src/display.c         SSD1306 (page-addressing) + DMA1-ch2 page-refresh engine
src/keys.c            PA0–PA7 keys + DFU strap
src/font.c            verbatim 5x7 font