# STM32MP257F Production Programming Guide

## Overview

This production programming system uses a **hybrid approach** for optimal speed:
1. **Initial Setup (STM32CubeProgrammer):** Flash boot infrastructure once - sets eMMC boot configuration
2. **Production Updates (PRG-TOOLBOX-FB):** Flash large filesystems at high speed via fastboot

## Initial Board Setup (Once Per Board)

**Use STM32CubeProgrammer GUI:**

1. Set device to DFU mode (BOOT switches: 0,1,0)
2. Power on device
3. Open STM32CubeProgrammer
4. Load TSV: `FlashLayout_boot_init.tsv`
5. Set binary directory: `./` (current directory)
6. Click "Download"
7. Wait for completion (~2-3 minutes)

**This writes:**
- Boot partitions (boot0/boot1) with TF-A
- Metadata partitions (FWU multi-bank support)
- FIP-A partition (U-Boot + OP-TEE)
- U-Boot environment
- bootfs partition (Linux kernel + device trees)
- **Sets eMMC PARTITION_CONFIG to 0x50 (boot from boot1)**

**Verify Boot:**
- Set BOOT switches to eMMC mode (0,0,1)
- Power cycle
- Device should boot to U-Boot (will fail to mount rootfs - expected)

## Production Filesystem Updates (Fast - Every Build)

Once boot infrastructure is initialized, use the fast PRG-TOOLBOX workflow:

```bash
./production_program_service.py -v
```

**This updates:**
- vendorfs (250MB) 
- rootfs (3GB)
- userfs (remaining space)

**Flashing speed:** ~60 seconds for 3.5GB via fastboot (vs 15+ minutes via DFU)

## TSV File Reference

### FlashLayout_boot_init.tsv
- **Purpose:** Initial board setup via STM32CubeProgrammer
- **Speed:** Slow (DFU mode)
- **Frequency:** Once per board
- **Sets:** eMMC boot configuration register

### FlashLayout_factory.tsv  
- **Purpose:** Stage 1 (DFU) - loads fastboot U-Boot into RAM
- **Contents:** RAM-only entries (no eMMC writes)

### FlashLayout_emmc_stm32mp257f-cargt-00395-00365v3-optee.tsv
- **Purpose:** Stage 2 (Fastboot) - writes large filesystems
- **Speed:** Fast (fastboot mode)
- **Frequency:** Every software update

## Boot Configuration Details

**PARTITION_CONFIG Register (EXTCSD[179]):**
- Value: 0x50
  - BOOT_ACK: 0x1 (boot acknowledge enabled)
  - BOOT_PARTITION_ENABLE: 0x2 (boot from boot1/partition 2)
  - PARTITION_ACCESS: 0x0 (access user area)

**This configuration persists across reflashes** - PRG-TOOLBOX can update filesystems without changing boot config.

## Troubleshooting

### Device won't boot after initial flash
1. Verify boot config in U-Boot (boot from USB):
   ```
   STM32MP> mmc dev 1
   STM32MP> mmc partconf 1
   ```
   Should show: `BOOT_PARTITION_ENABLE: 0x2`

2. If wrong, manually set:
   ```
   STM32MP> mmc partconf 1 1 2 0
   STM32MP> reset
   ```

### Production script fails
- Ensure board was initialized with STM32CubeProgrammer first
- Check that boot config is set (see above)
- Verify device enters fastboot mode after Stage 1

## Workflow Summary

```
┌─────────────────────────────────────┐
│ New Board (First Time)              │
├─────────────────────────────────────┤
│ 1. STM32CubeProgrammer              │
│    └─> Flash boot infrastructure   │
│    └─> Set PARTITION_CONFIG=0x50   │
│ 2. Verify boot to U-Boot            │
└─────────────────────────────────────┘
              ↓
┌─────────────────────────────────────┐
│ Software Updates (Every Build)      │
├─────────────────────────────────────┤
│ 1. production_program_service.py    │
│    └─> Stage 1: Load fastboot       │
│    └─> Stage 2: Flash filesystems   │
│ 2. Device boots updated software    │
└─────────────────────────────────────┘
```
