# Image Compilation

Pre-built images can be downloaded from the [K230 Linux SDK Image Center](https://download.kendryte.com/k230/release/linux_sdk_images/daily_build/).

## Overview

The K230 Linux SDK is a Buildroot-based Linux image build kit. This document provides two build methods:

- **Direct Native Compilation**: Manually set up the environment on Ubuntu
- **Build with Docker**: No need to install any build dependencies, one command completes the build

## Direct Native Compilation
>
>This SDK compilation has only been verified on ubuntu 20.04, 22.04, 24.04, other versions may have issues
>
### Get the Code

```bash
git clone git@github.com:kendryte/k230_linux_sdk.git  #If github is unstable, replace with git clone git@gitee.com:kendryte/k230_linux_sdk.git
# git clone git@gitee.com:kendryte/k230_linux_sdk.git
cd k230_linux_sdk
```

### Install Cross Toolchain

```bash
sudo make toolchain_and_depend
```

This command installs GCC and the dependency packages required by the SDK. For details, see `tools/install_toolchain_and_depend.sh`.

### Install ILP32 Toolchain (Optional)

Only required for the `k230d_canmv_ilp32_defconfig` configuration:

```bash
wget -c https://github.com/ruyisdk/riscv-gnu-toolchain-rv64ilp32/releases/download/2024.06.25/riscv64ilp32-elf-ubuntu-22.04-gcc-nightly-2024.06.25-nightly.tar.gz
mkdir -p /opt/toolchain/riscv64ilp32-elf-ubuntu-22.04-gcc-nightly-2024.06.25/
tar -xvf riscv64ilp32-elf-ubuntu-22.04-gcc-nightly-2024.06.25-nightly.tar.gz \
    -C /opt/toolchain/riscv64ilp32-elf-ubuntu-22.04-gcc-nightly-2024.06.25/
```

### Build

```bash
make CONF=k230d_canmv_defconfig       # k230d canmv image (both kernel and root filesystem are 64-bit)
# make CONF=k230_canmv_01studio_defconfig  # 01Studio board
# make CONF=k230_canmv_defconfig           # k230 canmv image
# make CONF=k230d_canmv_ilp32_defconfig    # k230d canmv 32-bit root filesystem
```

### Build Output

`output/<conf>/images/sysimage-sdcard.img.gz`

Need to decompress before flashing.

## Building with Docker

Use the preconfigured [k230-builder](https://github.com/huangzhenming/k230-builder) image, which includes all toolchains and build dependencies. The only prerequisite is installing Docker.

### Prerequisites

- **Install Docker**

  ```bash
  # Ubuntu/Debian
  sudo apt install docker.io
  sudo usermod -aG docker $USER && newgrp docker
  ```

  For other systems, refer to the [official Docker documentation](https://docs.docker.com/engine/install/).

- **Configure Git user information**

  ```bash
  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"
  ```

- **Configure SSH keys** (if cloning via SSH, optional)

### Install the k230 command

```bash
curl -fsSL https://www.kendryte.com/misc/install.sh | bash
source ~/.bashrc
```

After installation, run `k230 pull` to pull the Docker image (first time depends on network, approximately 1GB+).

### Quick Start

```bash
# 1) Clone the code
git clone git@gitee.com:kendryte/k230_linux_sdk.git
# Or clone from GitHub
# git clone git@github.com:kendryte/k230_linux_sdk.git
cd k230_linux_sdk

# 2) Pull the Docker image
k230 pull

# 3) Download toolchains (only needed the first time)
k230 download-toolchains TC2

# 4) Build (the CONF parameter specifies the configuration name)
k230 make CONF=k230_canmv_defconfig
```

After the build completes, the image output is in the `output/<conf>/images/` directory.

### Building ILP32 (32-bit Root Filesystem)

The ILP32 configuration requires downloading the TC4 toolchain additionally:

```bash
k230 download-toolchains TC4
k230 make CONF=k230d_canmv_ilp32_defconfig
```

### Common Commands

```bash
k230 list-toolchains                      # List available toolchains
k230 download-toolchains TC4              # Download the ILP32 toolchain (optional)
k230 make CONF=k230_canmv_defconfig       # Build with the specified configuration
k230 bash                                 # Enter the container terminal
k230 pull                                 # Update the k230-builder image
```

For available configuration names, see the `buildroot-overlay/configs/` directory in the source code.

### Git and SSH

`k230` automatically mounts the host's `~/.ssh` and `~/.gitconfig` into the container, so `git clone` works directly without additional configuration.

### Frequently Asked Questions

#### `k230` command not found

Run `source ~/.bashrc` or re-login to the terminal. Make sure `~/.local/bin` is in `$PATH`.

#### Toolchain download failure

The toolchains are stored in the Docker Volume `k230_toolchains`. The initial download is large. If the network is unstable, you can download in batches:

```bash
k230 download-toolchains TC2     # Download TC2 first
k230 download-toolchains TC4     # Then download TC4
```

#### Docker permission denied

```bash
sudo usermod -aG docker $USER
newgrp docker
```
