How I survived to a SoC with a terrible Linux BSP

Similar documents
How I survived to a SoC with a terrible Linux BSP

D1 - Embedded Linux. Building and installing an embedded and real-time Linux platform. Objectives. Course environment.

Embedded Linux system development training 5-day session

Booting Linux Fast & Fancy. Embedded Linux Conference Europe Cambridge, Robert Schwebel

EMBEDDED LINUX ON ARM9 Weekend Workshop

D1Y - Embedded Linux with Yocto

IPL+UBI: Flexible and Reliable with Linux as the Bootloader

LINUX KERNEL UPDATES FOR AUTOMOTIVE: LESSONS LEARNED

Adding hardware support to Buildroot

U-Boot Falcon Mode. Minimizing boot times using U-Boot "Falcon" mode. Stefano Babic / Wolfgang Denk. July 2012

D1S - Embedded Linux with Ac6 System Workbench

Embedded Linux Conference Europe Sascha Hauer

Track Two Building an Internet Radio with the TI Sitara AM3517 using LinuxLink

Track Three Building a Rich UI Based Dual Display Video Player with the Freescale i.mx53 using LinuxLink

ARM Powered SoCs OpenEmbedded: a framework for toolcha. generation and rootfs management

Flash filesystem benchmarks

Track One Building a connected home automation device with the Digi ConnectCore Wi-i.MX51 using LinuxLink

ARM64 + FPGA and more: Linux on the Xilinx ZynqMP

Lab 1. OMAP5912 Starter Kit (OSK5912)

First Steps. esom/sk4 esom/3517 Embedded Linux Starter Kit

Patrick Doyle Principal Software Engineer, irobot 2017 Embedded Linux Conference, Portland OR

The Right Approach to Minimal Boot Times

Hands-on with the Sitara Linux SDK

NAND/MTD support under Linux

Supporting a new ARM platform: the Allwinner example

UFCETW-20-2 Examination Answer all questions in Section A (60 marks) and 2 questions from Section B (40 marks)

Rootfs made easy with Buildroot

Porting Linux to a new SoC

Jade IdeaBoxx. Product Manual

Supporting a new ARM platform: the Allwinner example

OP-TEE Using TrustZone to Protect Our Own Secrets

Building a reference IoT product with Zephyr. Ricardo Salveti Michael Scott Tyler Baker

64 bit Bare Metal Programming on RPI-3. Tristan Gingold

DevKit8000 Evaluation Kit

Kernel Internals. Course Duration: 5 days. Pre-Requisites : Course Objective: Course Outline

Linux. For BCT RE2G2. User Guide. Document Reference: BCTRE2G2 Linux User Guide. Document Issue: Associated SDK release: 1.

Current Challenges in UBIFS

i.mx 7 - Hetereogenous Multiprocessing Architecture

Rethinking the core OS in 2015

Linux FastBoot. Reducing Embedded Linux Boot Times. Embedded World Conference 2012

ECE 471 Embedded Systems Lecture 12

Firmware Updates for Internet of Things Devices

iw-rainbow-g3 / G3V FAQs:

Getting Started with FreeRTOS BSP for i.mx 7Dual

using the Texas Instruments Zoom

Track Three Building a Rich UI Based Dual Display Video Player with the Freescale i.mx53 using LinuxLink

Embedded Systems. Mail: Web: Université de Nice - Sophia Antipolis

SparkGate7 Quick startup guide

MCAM335x Linux User's Guide

[537] Journaling. Tyler Harter

ECE 471 Embedded Systems Lecture 16

REAL TIME IMAGE PROCESSING BASED ON EMBEDDED LINUX

Porting U-Boot and Linux on new ARM boards: a step-by-step guide

FLASH Programming in Production of VS1000 Applications

U-Boot Bootloader for IoT Platform?

MYD-JA5D2X Development Board

Embedded Linux kernel and driver development training 5-day session

Porting FreeBSD to AArch64

Yocto Project and OpenEmbedded training 3-day session

1 FOSDEM like real computers - Making distributions work on single board computers André Przywara 04/02/2018

Introduction p. 1 Why Linux? p. 2 Embedded Linux Today p. 3 Open Source and the GPL p. 3 Free Versus Freedom p. 4 Standards and Relevant Bodies p.

From One Architecture to Many: Porting OpenMandriva to AArch64, armv7hnl, RISC-V and Ryzen. OSS/ELC 2018 Bernhard Bero Rosenkränzer

Embest SOC8200 Single Board Computer

Shared Logging with the Linux Kernel!!Part Deux!!

Birds of a Feather Session - OSS Vancouver Eystein Stenberg, Mender.io

BeagleLogic Documentation

January 28 29, 2014San Jose. Engineering Workshop

User Guide Linux for AT91CAP9-STK Version 1.1. User Guide LINUX FOR AT91CAP9-STK VERSION: 1.1 1/11

Linux Kernel Evolution. OpenAFS. Marc Dionne Edinburgh

ECE 598 Advanced Operating Systems Lecture 4

EMBEDDED SYSTEMS WITH ROBOTICS AND SENSORS USING ERLANG

CSE 333 Lecture 9 - storage

File Systems Management and Examples

The ultimate guide to software updates on embedded Linux devices

Update on boot time reduction techniques

2 Initialize a git repository on your machine, add a README file, commit and push

Advanced Operating Systems Embedded from Scratch: System boot and hardware access. Federico Terraneo

Linux Files and the File System

MYD-IMX28X Development Board

Running a Linux-Capable Open Source Soft SoC on the Avalanche Board with MicroSemi PolarFire FPGA

TQ2440 Development Platform Manual

Headless Android Android Builders Summit 2012 Karim

96Boards Enablement for opensuse

Raspberry Pi Cloud. Pete Stevens Mythic Beasts Ltd.

Bringing display and 3D to the C.H.I.P computer

LS9200 User Guide LinkSprite Technologies, Inc.

Azure Sphere: Fitting Linux Security in 4 MiB of RAM. Ryan Fairfax Principal Software Engineering Lead Microsoft

The Scratchbox development environment

U-Boot and Linux Kernel Debug using CCSv5

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

Oxalis Getting Started

An Incubator Project in the Apache Software Foundation. 13 July 2016

What is new? Page 1. Peter Korsgaard, Maintainer ELCE 2013

1 / 23. CS 137: File Systems. General Filesystem Design

First Steps. esom/sk5 esom/3517 Embedded Linux Starter Kit

i.mx 6UltraLite Evaluation Kit Quick Start Guide s datasheet has been downloaded from at this pag

Bringing display and 3D to the C.H.I.P computer

Introducing StellarisWare

Open Source Facebook. David Hendricks: Firmware Engineer Andrea Barberio: Production Engineer

FILE SYSTEMS, PART 2. CS124 Operating Systems Fall , Lecture 24

Transcription:

How I survived to a SoC with a terrible Linux BSP Working with jurassic vendor kernels, missing pieces and buggy code Luca Ceresoli luca@lucaceresoli.net http://lucaceresoli.net FOSDEM 2017

About me Open source enthusiast Contributor to Buildroot and a few other projects Embedded Linux engineer Develop real products on custom hardware Kernel, bootloader, drivers Integration, build system 1

Introduction 2

Typical embedded Linux system A physical product based on an ad-hoc electronic board Built around a System-on-Chip (SoC) 3

The System on Chip Nuvoton N32926 Cheap ARM926EJ-S @ 240 MHz Peripherals: H.264 en/decoder, Ethernet MAC, USB, CMOS sensor interface, video out, LCD controller, sound, 64 MB DDR2 on package LQFP package Source: https://www.nuvoton.com/hq/products/microprocessors/arm9-mpus/n3292-h. 264-codec-series/n32926u1dn DDR2 SoC 4

The ideal BSP BSP = Board Support Package The ideal BSP Mainline kernel Mainline U-Boot or Barebox Good hardware documentation Why? All standard, open-source components Well known quality Community and commercial support Maintained (bugfixes!) Reuse components Infrastructure from other products Any open source package (including those on your PC) 5

The Quest 6

The quest Documentation Linux kernel Toolchain Booting Tools Customer support 7

Documentation 8

Public documentation Website:https://www.nuvoton.com/hq/products/microprocessors/arm9-mpus/n3292-h. 264-codec-series/n32926u1dn An 8-page datasheet (mostly a list of features) 9

Documentation for customers Only under NDA 10

Accessible documentation A low-cost devkit is available from chinese online stores Contains a DVD-ROM with a subset of the BSP for customers Documentation and software Contains the N3292x Design Guide SoC peripherals (registers) 11

Linux kernel 12

Vendor kernel VS mainline kernel Base kernel: Linux 2.6.35.4 (2010) 2.6.35.4 2.6.35.14 (latest stable) 11 months 1382 bugfix commits Merged with minimal conflicts 2.6.35.14 4.9 (latest mainline) A countless number of bugfixes, performance improvements, new features Security Device Tree 13

Vendor kernel additions Provided as patches: w55fa92-kernel-2.6.35-000.patch (3.6 MB) w55fa92-kernel-2.6.35-001.patch (1.4 MB) w55fa92-kernel-2.6.35-002.patch (0.4 MB) do_kernel_patch.sh Total: 170.000 lines changed 14

Vendor kernel issues 1. Bugs 2. Missing features 3. Code quality 15

Bugs Examples: Sound Processing Unit ALSA driver arecord myfile.wav kernel crash NULL pointer dereference H.264 decoder driver Works with sample streams Kernel crash on streaming packet loss Several NULL pointer dereferences 16

Missing features Examples: GPIO Basic functionality is implemented No interrupt handling Power Management Implemented with a proprietary API Also implemented the Linux standard way, but incomplete and not working 17

Code quality Average quality of additions: generally bad Trivial metric: +521 lines starting with #if 0 A few examples follow 18

Code quality: driver model drivers/video/w55fa92_fb.c: #ifdef CONFIG_GIANTPLUS_GPM1006D0_320X240 #include "w55fa92_giantplus_gpm1006d0.c" #endif #ifdef CONFIG_TOPPLY_320X240 #include "w55fa92_topply_320x240.c" #endif /*...5 more displays... */ #if 0 #ifdef CONFIG_SHARP_LQ035Q1DH02_320X240 #include "w55fa92_sharp_lq035q1dh02.c" #endif #ifdef CONFIG_WINTEK_WMF3324_320X240 #include "w55fa92_wintek_wmf3324.c" #endif /*...5 more displays... */ #endif 19

Code quality: H.264 codec memory allocation drivers/misc/codec264/favc_module.c: unsigned int get_avc_buffer_size(void) { /*...~90 lines... */ return TOTAL_VDE_BUF_SIZE; } EXPORT_SYMBOL(get_avc_buffer_size); From arch/arm/mm/mmu.c: extern unsigned int get_avc_buffer_size(void); void init reserve_node_zero(pg_data_t *pgdat) { /*... */ buffer_size = get_avc_buffer_size(); printk("avc Buffer Size: 0x%x\n",buffer_size); w55fa92_vde_v = alloc_bootmem_low_pages (buffer_size); /*... */ } 20

Versioning vendor drops fixes develop fixes vendor patch 2 fixes vendor patch 1 fixes vendor patch 0 2.6.35.14 21

Toolchain 22

Vendor toolchain The BSP provides a toolchain. Why? What s inside gcc 4.2.1 (July 2007) No C++11 support gcc 4.2.x got fixes until 4.2.4 (May 2008) uclibc 0.9.29 (2007) What if I need glibc or musl? Bugfixes and improvements in later versions? A few other libraries (libcurl, libpng ) A hand-crafted script to install it at a hard-coded location 23

Toolchain selection Don t use the provided toolchain You could use a pre-built toolchain If it has been built with kernel headers <= 2.6.35 So it s probably quite old itself Build you own crosstool-ng, Buildroot, Openembedded 24

Booting 25

Bootloaders in the BSP No U-Boot No Barebox Some proprietary bootloaders Sources provided Not open source 26

Vendor booting scheme (NAND) SPL U-Boot env root fs (UBIFS) kernel UBI A common booting scheme BOOT ROM expose FAT as USB mass storage NAND Loader NVT Loader conprog.bin Image (with initramfs) FAT1 start.sh FAT2 Vendor (demo) booting scheme 27

Vendor booting scheme pros Easy deployment of demos provided by vendor 1. Press a button during boot 2. Mount mass storage on PC 3. Replace files 28

Vendor booting scheme issues /1 FAT Unreliable on power loss It just cannot contain a UNIX-like rootfs (no users, groups, permissions, symlinks...) NAND FTL FAT-on-NAND emulation (with FTL) is in a binary module NVT Loader cannot mount UBIFS No provision for redundancy: one copy of each component 29

Vendor booting scheme issues /2 Root filesystem is an initramfs Changes are volatile Limited size, everything in RAM Persistent changes stay in FAT Nobody passes cmdline to kernel it must be hard-coded in the kernel (CONFIG_CMDLINE) NFS booting Needs cmdline parameters must rebuild and reflash the kernel Cannot load kernel via TFTP 30

Alternative booting options? 31

Option 1: add a SquashFS layer on top of FAT NAND Loader NVT Loader FAT1 conprog.bin Image (with initramfs) start.sh squashfs Keep the existing structure untouched Remove FAT space constraint and RAM usage Still read-only ext2 or any other rw filesystem over FAT over NAND is crazy The device cannot atomically upgrade itself 32

Option 2: jump from FAT to UBIFS NAND Loader NVT Loader FAT1 conprog.bin Image (with initramfs) UBI root fs (UBIFS) UBI and UBIFS are designed for NAND! (efficient, reliable, quite scalable) Tweaks needed Change the initramfs /init to mount UBIFS and switch_root Tweak NVT Loader not to use all space for FAT USB mass storage can only update kernel FAT area atrophied, NVT Loader almost useless 33

Option 3: skip NVT Loader NAND Loader Image + initramfs UBI root fs (UBIFS) NAND Loader loads kernel Image to address 0 and jump there No more NVT Loader and FAT Less code, less bugs, faster boot, more free space Kernel still on bare NAND and without cmdline Safe kernel upgrade? kexec 34

Option 4: Port U-Boot Port U-Boot or Barebox to the SoC Maybe keeping the vendor NAND Loader (SPL) Unleashes all the known advantages Environment, boot-time scripting, prompt, cmdline, TFTP boot Redundancy for all/most components on bare NAND Time to market? 35

Tools 36

Tools Which tools do I need? Ideally, none Flashing an empty memory is different Some vendors have proprietary tools 37

Flashing tools Tool provided to write memory Quite flexible Can write NAND, SPI, SD, SDRAM (and execute) Over USB For Windows only Proprietary GUI, not scriptable Protocol to Boot ROM not documented You re locked to it NAND Windows PC USB BOOT ROM NOR SD RAM 38

NAND partition table Proprietary partition table in the NAND Loader area The proprietary tool writes only this format Not a bad idea but standard tools work differently NAND Loader You can t get rid of the table 39

Customer support 40

Customer support Standard, mainline code Plenty of options for community and commercial support Proprietary code with sources Vendor support Read the code Proprietary, binary software (and hardware issues) One choice: vendor support Still acceptable if support is good But don t bet it will be 41

Customer support issues The engineer who knows the answer is hidden by reseller sales dept., FAE, customer support department Responsiveness Timezone issues 42

Customer support a bad example A real conversation (short form) Me The proprietary tool doesn t work CS Works on my PC, see screenshot Me Not on mine; can it log errors so you can diagnose it? CS Adding logging would not be practical 43

Concluding remarks 44

The result Comparison with a well-supported SoC The product works Final quality is lower The hardware would allow to do better Extra time spent Sometimes we supported ourselves Look for stuff in the BSP Fix bugs Reinvent booting 45

What can I do to improve things? What can I do to make a better world? As an embedded Linux engineer Assess potential problem early while evaluating a SoC Especially booting and hardware support As a hobbyist or a hacker Pick boards with good mainline support, or Improve existing support and mainline it 46

What can vendors do to ship better BSPs? Happy engineer = good product = more sales Don t reinvent the wheel Write good docs, no NDA, no registration Including yout Boot ROM protocol And let people write the tools they want Push your code to mainline (or outsource this to a specialized company) Expensive, but rewarding Somebody else will update, fix, improve and support it Leverage the community Let your engineers use mailing-lists, IRC etc Make cheap, hacker-friendly boards 47

Thank you for your attention Questions? luca@lucaceresoli.net http://lucaceresoli.net Copyright 2017, Luca Ceresoli Slides released under Creative Commons Attribution - Share Alike 3.0 License https://creativecommons.org/licenses/by-sa/3.0/ 48

Extra Slides

Using an old gcc an example A C++ program using libconfuse 3.0 #include <confuse.h> //... cfg_opt_t opts[] = { CFG_STR("my-param", "defval", CFGF_NONE), CFG_END() }; With gcc <= 4.8 fails building due to designated initializers not being implemented: error: expected primary-expression before '.' token

Kernel code quality example 1 Changes to Makefile: -ARCH?= $(SUBARCH) -CROSS_COMPILE?= -CROSS_COMPILE?= $(CONFIG_CROSS_COMPILE:''%''=%) +#ARCH?= $(SUBARCH) +ARCH= arm +#CROSS_COMPILE?= +#CROSS_COMPILE?= $(CONFIG_CROSS_COMPILE:''%''=%) +CROSS_COMPILE= arm-linux- Prevents using toolchains with a different prefix Any advantage?

Kernel code quality example 2 Changes to arch/arm/boot/makefile: $(obj)/image: vmlinux FORCE $(call if_changed,objcopy) @echo ' Kernel: $@ is ready' +ifeq ($(CONFIG_ARCH_W55FA92),y) + cp $@../image/conprog.bin +endif../image/ does not make sense in any buildsystem

Kernel code quality example 3 sound/soc/w55fa92/w55fa92_spu.c: if (nchannels ==1) { DrvSPU_EnableInt(_u8Channel0, DRVSPU_ENDADDRESS_INT); DrvSPU_EnableInt(_u8Channel0, DRVSPU_THADDRESS_INT); } else { /* just open one channel interrupt */ DrvSPU_EnableInt(_u8Channel0, DRVSPU_ENDADDRESS_INT); DrvSPU_EnableInt(_u8Channel0, DRVSPU_THADDRESS_INT); } Find the differences between the then and the else branch

Kernel code quality example 4 sound/soc/w55fa92/w55fa92_spu.c: static int DrvSPU_EnableInt(u32 u32channel, u32 u32interruptflag) { if ( (u32channel >=edrvspu_channel_0) && (u32channel <=edrvspu_channel_31) ) { /*... */ if (u32interruptflag & DRVSPU_USER_INT) { AUDIO_WRITE(REG_SPU_CH_EVENT, AUDIO_READ(REG_SPU_CH_EVENT) EV_USR_EN); } if (u32interruptflag & DRVSPU_SILENT_INT) { AUDIO_WRITE(REG_SPU_CH_EVENT, AUDIO_READ(REG_SPU_CH_EVENT) EV_SLN_EN); } /*...a few more times... */ /*... */ return E_SUCCESS; } else return E_DRVSPU_WRONG_CHANNEL; }

Kernel code quality example 5 arch/arm/mach-w55fa92/include/mach/w55fa92_gpio.h: static inline int w55fa92_gpio_configure(int group, int num) { /*... */ case GPIO_GROUP_B: if(num <= 7) writel(readl(reg_gpbfun0) &~ (0xF << (num<<2)), REG_GPBFUN0); else writel(readl(reg_gpbfun1) &~ (0xF << ((num%8)<<2)), REG_GPBFUN1); break; case GPIO_GROUP_C: if(num <= 7) writel(readl(reg_gpcfun0) &~ (0xF << (num<<2)), REG_GPCFUN0); else writel(readl(reg_gpcfun1) &~ (0xF << ((num%8)<<2)), REG_GPCFUN1); break; /*...similarly fo other GPIO ports... */ } A little refactoring would help