# r0rt1z2/fenrir architecture

> Architecture diagram of the GitHub repository r0rt1z2/fenrir, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/r0rt1z2/fenrir
- Repository: https://github.com/r0rt1z2/fenrir

## Overview

Fenrir is an archived MediaTek bootloader patching and payload-injection tool. The documented workflow takes a device-specific LK bootloader image, applies configured security and lock-state patches and optional payload stages, bypasses certificate checks for modified partitions, and writes a patched image for flashing. The graph also shows the separate in-bootloader payload capabilities. Source excerpts establish the injector and payload relationships; unsampled build/flash scripts are not modeled as runtime components.

Main entry points: [`injector/injector.py`](https://github.com/r0rt1z2/fenrir/blob/main/injector/injector.py) (Image injector), [`payload`](https://github.com/r0rt1z2/fenrir/tree/main/payload) (Stage payloads), [`payload/stage3/stage3.c`](https://github.com/r0rt1z2/fenrir/blob/main/payload/stage3/stage3.c) (Linux-init stage), [`injector/stage.py`](https://github.com/r0rt1z2/fenrir/blob/main/injector/stage.py) (Stage execution)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_host["Host Injector"]
  node_cli["Injector entry<br/>[inject.py]"]
  node_injector["Image injector<br/>[injector.py]"]
  node_certs["Certificate bypass<br/>[cert_bypass.py]"]
end

subgraph group_patch["Patch Configuration"]
  node_device_defs["Device profiles<br/>[devices.py]"]
  node_device_model["Device model<br/>[device.py]"]
  node_stages["Stage execution<br/>[stage.py]"]
  node_matcher["Pattern matching<br/>[patch_utils.py]"]
end

subgraph group_payload["Bootloader Payload"]
  node_payload_bundle["Stage payloads"]
  node_stage1["Platform-init stage<br/>[stage1.c]"]
  node_stage2["Fastboot stage<br/>[stage2.c]"]
  node_stage3["Linux-init stage<br/>[stage3.c]"]
end

subgraph group_support["Payload Runtime"]
  node_device_addresses["Device addresses<br/>[pacman.h]"]
  node_device_config["Payload device config<br/>[device_config.h]"]
  node_common_api["Common API<br/>[common.h]"]
  node_debug_api["Debug API<br/>[debug.h]"]
  node_fastboot_api["Fastboot API<br/>[fastboot.h]"]
  node_mmu_api["MMU API<br/>[mmu.h]"]
  node_arm64["ARM64 definitions<br/>[arm64.h]"]
  node_runtime_strings["String helpers<br/>[string.h]"]
end

node_operator(("Operator"))
node_bootloader["LK image"]
node_patched["Patched image"]
node_flash(("Device flashing"))

node_operator -->|"provides image"| node_bootloader
node_bootloader -->|"input image"| node_cli
node_cli -->|"selects profile"| node_device_defs
node_device_defs -->|"defines devices"| node_device_model
node_device_defs -->|"configures stages"| node_stages
node_device_model -->|"uses injector"| node_injector
node_injector -->|"executes stages"| node_stages
node_stages -->|"matches patterns"| node_matcher
node_injector -->|"applies bypass"| node_certs
node_injector -->|"writes image"| node_patched
node_patched -->|"flash image"| node_flash
node_stages -->|"loads payload"| node_payload_bundle
node_payload_bundle -.->|"contains stage"| node_stage1
node_payload_bundle -.->|"contains stage"| node_stage2
node_payload_bundle -.->|"contains stage"| node_stage3
node_device_config -->|"selects addresses"| node_device_addresses
node_stage1 -->|"uses logging"| node_debug_api
node_stage2 -->|"uses logging"| node_debug_api
node_stage2 -->|"uses fastboot"| node_fastboot_api
node_stage3 -->|"uses logging"| node_debug_api
node_stage3 -->|"uses MMU"| node_mmu_api
node_stage3 -->|"uses strings"| node_runtime_strings
node_common_api -->|"includes definitions"| node_arm64
node_debug_api -->|"includes API"| node_common_api
node_fastboot_api -->|"includes API"| node_common_api
node_mmu_api -->|"includes API"| node_common_api

click node_cli "https://github.com/r0rt1z2/fenrir/blob/main/injector/inject.py"
click node_device_defs "https://github.com/r0rt1z2/fenrir/blob/main/injector/devices.py"
click node_device_model "https://github.com/r0rt1z2/fenrir/blob/main/injector/device.py"
click node_injector "https://github.com/r0rt1z2/fenrir/blob/main/injector/injector.py"
click node_stages "https://github.com/r0rt1z2/fenrir/blob/main/injector/stage.py"
click node_matcher "https://github.com/r0rt1z2/fenrir/blob/main/injector/patch_utils.py"
click node_certs "https://github.com/r0rt1z2/fenrir/blob/main/injector/cert_bypass.py"
click node_payload_bundle "https://github.com/r0rt1z2/fenrir/tree/main/payload"
click node_stage1 "https://github.com/r0rt1z2/fenrir/blob/main/payload/stage1/stage1.c"
click node_stage2 "https://github.com/r0rt1z2/fenrir/blob/main/payload/stage2/stage2.c"
click node_stage3 "https://github.com/r0rt1z2/fenrir/blob/main/payload/stage3/stage3.c"
click node_device_addresses "https://github.com/r0rt1z2/fenrir/blob/main/payload/devices/pacman.h"
click node_device_config "https://github.com/r0rt1z2/fenrir/blob/main/payload/common/device_config.h"
click node_common_api "https://github.com/r0rt1z2/fenrir/blob/main/payload/common/common.h"
click node_debug_api "https://github.com/r0rt1z2/fenrir/blob/main/payload/common/debug.h"
click node_fastboot_api "https://github.com/r0rt1z2/fenrir/blob/main/payload/common/fastboot.h"
click node_mmu_api "https://github.com/r0rt1z2/fenrir/blob/main/payload/common/mmu.h"
click node_arm64 "https://github.com/r0rt1z2/fenrir/blob/main/payload/common/arm64.h"
click node_runtime_strings "https://github.com/r0rt1z2/fenrir/blob/main/payload/common/string.h"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_injector,node_certs toneBlue
class node_device_defs,node_device_model,node_stages,node_matcher toneAmber
class node_payload_bundle,node_stage1,node_stage2,node_stage3 toneMint
class node_device_addresses,node_device_config,node_common_api,node_debug_api,node_fastboot_api,node_mmu_api,node_arm64,node_runtime_strings toneRose
class node_operator,node_bootloader,node_patched,node_flash toneIndigo
```

## Components

### Host Injector

- [Injector entry](https://github.com/r0rt1z2/fenrir/blob/main/injector/inject.py): `injector/inject.py`
- [Image injector](https://github.com/r0rt1z2/fenrir/blob/main/injector/injector.py): `injector/injector.py`
- [Certificate bypass](https://github.com/r0rt1z2/fenrir/blob/main/injector/cert_bypass.py): `injector/cert_bypass.py`

### Patch Configuration

- [Device profiles](https://github.com/r0rt1z2/fenrir/blob/main/injector/devices.py): `injector/devices.py`
- [Device model](https://github.com/r0rt1z2/fenrir/blob/main/injector/device.py): `injector/device.py`
- [Stage execution](https://github.com/r0rt1z2/fenrir/blob/main/injector/stage.py): `injector/stage.py`
- [Pattern matching](https://github.com/r0rt1z2/fenrir/blob/main/injector/patch_utils.py): `injector/patch_utils.py`

### Bootloader Payload

- [Stage payloads](https://github.com/r0rt1z2/fenrir/tree/main/payload): `payload`
- [Platform-init stage](https://github.com/r0rt1z2/fenrir/blob/main/payload/stage1/stage1.c): `payload/stage1/stage1.c`
- [Fastboot stage](https://github.com/r0rt1z2/fenrir/blob/main/payload/stage2/stage2.c): `payload/stage2/stage2.c`
- [Linux-init stage](https://github.com/r0rt1z2/fenrir/blob/main/payload/stage3/stage3.c): `payload/stage3/stage3.c`

### Payload Runtime

- [Device addresses](https://github.com/r0rt1z2/fenrir/blob/main/payload/devices/pacman.h): `payload/devices/pacman.h`
- [Payload device config](https://github.com/r0rt1z2/fenrir/blob/main/payload/common/device_config.h): `payload/common/device_config.h`
- [Common API](https://github.com/r0rt1z2/fenrir/blob/main/payload/common/common.h): `payload/common/common.h`
- [Debug API](https://github.com/r0rt1z2/fenrir/blob/main/payload/common/debug.h): `payload/common/debug.h`
- [Fastboot API](https://github.com/r0rt1z2/fenrir/blob/main/payload/common/fastboot.h): `payload/common/fastboot.h`
- [MMU API](https://github.com/r0rt1z2/fenrir/blob/main/payload/common/mmu.h): `payload/common/mmu.h`
- [ARM64 definitions](https://github.com/r0rt1z2/fenrir/blob/main/payload/common/arm64.h): `payload/common/arm64.h`
- [String helpers](https://github.com/r0rt1z2/fenrir/blob/main/payload/common/string.h): `payload/common/string.h`

### Other components

- **Operator**
- **LK image**
- **Patched image**
- **Device flashing**

## Connections

- Operator → LK image: provides image (evidence: `README.md`)
- LK image → Injector entry: input image (evidence: `README.md`)
- Injector entry → Device profiles: selects profile (evidence: `injector/inject.py`)
- Device profiles → Device model: defines devices (evidence: `injector/devices.py`)
- Device profiles → Stage execution: configures stages (evidence: `injector/devices.py`)
- Device model → Image injector: uses injector (evidence: `injector/device.py`)
- Image injector → Stage execution: executes stages (evidence: `injector/injector.py`)
- Stage execution → Pattern matching: matches patterns (evidence: `injector/stage.py`)
- Image injector → Certificate bypass: applies bypass (evidence: `injector/injector.py`)
- Image injector → Patched image: writes image (evidence: `injector/injector.py`)
- Patched image → Device flashing: flash image (evidence: `README.md`)
- Stage execution → Stage payloads: loads payload (evidence: `injector/stage.py`)
- Stage payloads → Platform-init stage: contains stage (evidence: `payload/stage1/stage1.c`)
- Stage payloads → Fastboot stage: contains stage (evidence: `payload/stage2/stage2.c`)
- Stage payloads → Linux-init stage: contains stage (evidence: `payload/stage3/stage3.c`)
- Payload device config → Device addresses: selects addresses (evidence: `payload/common/device_config.h`)
- Platform-init stage → Debug API: uses logging (evidence: `payload/stage1/stage1.c`)
- Fastboot stage → Debug API: uses logging (evidence: `payload/stage2/stage2.c`)
- Fastboot stage → Fastboot API: uses fastboot (evidence: `payload/stage2/stage2.c`)
- Linux-init stage → Debug API: uses logging (evidence: `payload/stage3/stage3.c`)
- Linux-init stage → MMU API: uses MMU (evidence: `payload/stage3/stage3.c`)
- Linux-init stage → String helpers: uses strings (evidence: `payload/stage3/stage3.c`)
- Common API → ARM64 definitions: includes definitions (evidence: `payload/common/common.h`)
- Debug API → Common API: includes API (evidence: `payload/common/debug.h`)
- Fastboot API → Common API: includes API (evidence: `payload/common/fastboot.h`)
- MMU API → Common API: includes API (evidence: `payload/common/mmu.h`)
