# Bootloader

DepthAI bootloader is a small program which handles the booting process, either by initializing the OAK PoE camera so DepthAI API
can connect to it. OAK PoE cameras already come with factory bootloader flashed at the factory.

Bootloader is bundled inside with the `depthai` library, so to eg. flash the newest bootloader, you should use the newest
`depthai` library.

## Manage with oakctl

Use the [`oakctl rvc2` device manager](https://docs.luxonis.com/software-v3/depthai/depthai-components/bootloader.md) to discover
RVC2 cameras, inspect bootloader information, configure USB or network settings, update bootloaders, and recover devices.

```bash
oakctl rvc2 list
oakctl rvc2 info --device <MXID-or-IP>
oakctl rvc2 bootloader update --device <MXID-or-IP>
```

The update command uses the bootloader bundled with your installed oakctl version. On devices that support a user bootloader, it
updates the recoverable user bootloader and leaves the factory bootloader unchanged. Otherwise, it flashes the factory bootloader.
The command asks for confirmation before either operation; there is no second prompt for factory flashing. Use `--yes` to skip
confirmation. Keep the camera powered and connected until flashing completes.

For persistent configuration, factory bootloader flashing, application removal, and factory reset, follow the [RVC2 device
management guide](https://docs.luxonis.com/software-v3/depthai/depthai-components/bootloader.md).

## Factory and User bootloader

There are two types of bootloaders:

 * Factory bootloader: bootloader that is flashed in the factory. We don't recommend re-flashing this bootloader, as it is not
   meant to be edited by end users.
 * User bootloader: bootloader that can be flashed by the user. If booting is unsuccessful (eg. gets corrupted when flashing), it
   will fallback to factory bootloader.

USB devices don't support user bootloader. If device has User bootloader, it will be used by default. If user bootloader is not
flashed, it will fallback to Factory bootloader.

## Boot switches

 * Boot from flash - DIP switch: 0x03 (switches 5,6 ON) - used by OAK PoE and USB cameras when bootloader is installed.
 * Recovery mode for USB - DIP switch: 0x16 (switches 2,4,5 ON) - to boot directly into USB mode, so camera waits for the host to
   connect to it via USB.

On newer versions of OAK devices we have 0 ohm resistors (see image below) instead of a DIP switch, which means OAK will boot into
flash by default. These new devices have the bootloader flashed, which handles the booting process. There's also an additional
button on the baseboard that switches boot to recovery mode when pressed, which can be useful if the bootloader hasn't yet been
flashed (eg. early access devices). You need to press this button when powering the device (when booting happens).

## API

DeviceBootloader is a class used to communicate with the bootloader, flash a created
[Pipeline](https://docs.luxonis.com/software/depthai-components/pipeline.md), or update the bootloader itself. The DAP-related API
remains documented only as a legacy reference; RVC2 standalone deployments are unsupported.

```cpp
std::tuple<bool, std::string> flash(std::function<void(float)> progressCb, Pipeline& pipeline);

std::tuple<bool, std::string> flashDepthaiApplicationPackage(std::function<void(float)> progressCb, std::vector<uint8_t> package);

std::tuple<bool, std::string> flashBootloader(std::function<void(float)> progressCb, std::string path = "");
```

progressCb parameter takes a callback function, which will be called each time an progress update occurs (rate limited to 1
second). This is mainly used to inform the user of the current flashing progress.

### DepthAI Application Package (.dap)

> **Deprecated and unsupported**
> DepthAI Application Packages (`.dap`) and RVC2 standalone deployments are deprecated and no longer supported. Use peripheral mode for [RVC2-based](https://docs.luxonis.com/hardware/platform/rvc/rvc2.md) devices. For standalone deployments, use [RVC4-based](https://docs.luxonis.com/hardware/platform/rvc/rvc4.md) devices with [OAK Apps](https://docs.luxonis.com/software-v3/oak-apps.md).

DepthAI Application Package is a legacy binary file format which stores sections of data. The purpose of this format was to be
able to extract individual sections and do OTA updates without requiring to update all data. Example: Between update 1 and 2 of
users application, DepthAI firmware, Asset storage (50MiB neural network) and asset structure remained the same, but some
additional processing nodes were added to the pipeline. Instead of transferring the whole package, only Pipeline description can
be sent and updated.

DepthAI application package (.dap) consists of:

 * SBR (512B header which describes sections of data)
 * DepthAI device firmware (section "__firmware")
 * Pipeline description (section "pipeline")
 * Assets structure (section "assets")
 * Asset storage (section "asset_storage")

## MAC address

All OAK PoE cameras have a unique MAC address which is used to identify the device on the network. It is calculated from the MxID
of the device, see [logic here](https://gist.github.com/Erol444/85d6ef7ae9c946f5688854b86c548dc7). The MAC address is stored in
the DeviceBootloader configuration.
