# How to Flash Firmware

This document assumes you have completed compilation and obtained image files in `output/<defconfig>/images/`.

## Common Image File Types

- `*.kdimg` - recommended for K230BurningTool (USB)
- `*.img` - recommended for SD card writing and some command-line flashing tools

K230 supports four boot media: eMMC, SD card, SPI Nor Flash, and SPI Nand Flash. Firmware can be flashed using two methods:

- **USB flashing** - direct USB connection to the device
- **SD card flashing** - write image to SD card and boot from it

For serial and USB connection, refer to the [K230 Development Board](https://www.kendryte.com/products) documentation.

## USB Flashing

### Three Methods to Enter Flashing Mode

1. **No valid firmware on default boot media**
   - Example: fresh eMMC with no prior firmware, or SD card removed

1. **Force boot mode via Boot button**
   - Hold the board's **Boot button** while powering on to force flashing mode

1. **Trigger via terminal command**
   - Run on an already-booted RtSmart terminal:

     ```bash
     reboot_to_upgrade
     ```

   - The device will automatically reboot into flashing mode

> **Note**
> If `reboot_to_upgrade` does not enter flashing mode, use the latest flashing tool to flash the patch file:
> [K230 Patch/K230D Patch](https://kendryte-download.canaan-creative.com/developer/tools/chip_patch/)

### Using K230BurningTool (GUI Tool)

- **Download**: [K230BurningTool](https://www.kendryte.com/en/resource?selected=0-2-2)
- **Features**: supports multi-device flashing with intuitive interface
- **Steps**:
  1. Connect device and enter flashing mode
  1. Select firmware file (recommended `*.kdimg`)
  1. Click **Start Flashing**; after completion, click **Confirm** to proceed

### Using K230-Flash (Command-line Tool)

- **Installation**:

  ```bash
  pip install k230-flash
  ```

- **Features**: based on Kburn and Python, suitable for automated operations
- **Example usage**:

  ```bash
  # Linux / macOS
  k230_flash -m EMMC 0 output/<defconfig>/images/<your_image>.img

  # Windows
  k230_flash.exe -m EMMC 0 output\<defconfig>\images\<your_image>.img
  ```

## SD Card Flashing

### Using Rufus or balenaEtcher

- **Use case**: write image to SD card and boot the device
- **Supported formats**: raw image files (decompress `.gz` format first)
- **Tools**:
  - [Rufus](https://rufus.ie/en/)
  - [balenaEtcher](https://etcher.balena.io/)
- **Steps**: refer to [01Studio Firmware Flashing Guide](https://www.kendryte.com/k230_canmv/zh/main/zh/userguide/how_to_burn_firmware.html)

> **Important**
> `.kdimg` format **only supports K230BurningTool** and cannot be written via SD card tools.

## Summary

- **USB flashing**: quick device update, recommended `K230BurningTool` or `K230-Flash`
- **SD card flashing**: for devices without USB or bulk writing scenarios, use `Rufus` or `balenaEtcher`
