9
0
Fork 0
barebox bootloader for sysmobts
Go to file
Sascha Hauer b52c174b2d imx-bbu-nand-fcb: Make robust against power cuts
This patch makes the update to Nand robust against power
failures. With this we make sure that during every step of the
update at least one of the two images on Nand is readable and
valid.

Also this patch makes it possible to refresh/repair the boot
images on Nand. This may become necessary when a previous update
has been interrupted due to a power cut, or when the number of
bitflips is near to the number we can correct. This is also done
in a way that allow power cuts at every step.

We assume the following layout in the Nand flash:

 fwmaxsize = (n_blocks - 4) / 2

 block

 0              ----------------------
                | FCB/DBBT 0         |
 1              ----------------------
                | FCB/DBBT 1         |
 2              ----------------------
                | FCB/DBBT 2         |
 3              ----------------------
                | FCB/DBBT 3         |
 4              ----------------------
                | Firmware slot 0    |
 4 + fwmaxsize  ----------------------
                | Firmware slot 1    |
                ----------------------

When the layout found on the device differs from the above the update
won't be robust, but nevertheless works. Since the layout is changed
to the above during the update, the next update will be robust.

Here's the strategy we use to implement a robust update:

The FCBs contain pointers to the firmware slots in the
Firmware1_startingPage and Firmware2_startingPage fields. Note that
Firmware1_startingPage doesn't necessarily point to slot 0. We
exchange the pointers during update to atomically switch between the
old and the new firmware.

- We read the first valid FCB and the firmware slots.
- We check which firmware slot is currently used by the ROM:
  - if no FCB is found or its layout differs from the above layout,
    continue without robust update
  - if only one firmware slot is readable, the ROM uses it
  - if both slots are readable, the ROM will use slot 0
- Step 1: erase/update the slot currently unused by the ROM
- Step 2: Update FCBs/DBBTs, thereby letting Firmware1_startingPage
          point to the slot we just updated. From this moment
          on the new firmware will be used and running a
          refresh/repair after a power failure after this
          step will complete the update.
- Step 3: erase/update the other firmwre slot
- Step 4: Eventually write FCBs/DBBTs again. This may become
          necessary when step 3 revealed new bad blocks.

Refreshing the firmware which is needed when when blocks
become unreadable due to read disturbance works the same way,
only that the new firmware is the same as the old firmware
and that it will only be written when reading from the device
returns -EUCLEAN indicating that a block needs to be
rewritten.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2016-04-08 13:35:32 +02:00
Documentation bbu: Allow to refresh/repair images 2016-04-08 13:35:32 +02:00
arch Merge branch 'for-next/misc' 2016-03-11 10:49:50 +01:00
commands bbu: Allow to refresh/repair images 2016-04-08 13:35:32 +02:00
common imx-bbu-nand-fcb: Make robust against power cuts 2016-04-08 13:35:32 +02:00
crypto Merge branch 'for-next/ratp' 2016-02-08 08:27:01 +01:00
defaultenv defaultenv2: Add automount for RATPFS 2016-01-18 09:26:09 +01:00
drivers mtd: nand: export nand_check_erased_buf 2016-04-08 13:35:32 +02:00
dts dts: update to v4.5-rc5 2016-02-29 08:10:52 +01:00
fs mtd: Add support for marking blocks as good 2016-03-15 08:56:53 +01:00
images Merge branch 'for-next/imx' 2016-03-11 10:49:49 +01:00
include mtd: nand: export nand_check_erased_buf 2016-04-08 13:35:32 +02:00
lib remove now unused libmtd 2016-04-06 10:17:52 +02:00
net Merge branch 'for-next/rtc' 2016-01-11 13:11:08 +01:00
pbl pbl: console: Use console_putc to output the carriage return 2015-11-11 17:03:37 +01:00
scripts scripts: make __init__.py nonempty 2016-02-12 08:44:30 +01:00
.gitattributes add alternating mechanism and flash scripts for ipe337 2008-02-15 11:38:31 +01:00
.gitignore include pyserial trunk 2016-01-18 09:25:50 +01:00
.mailmap .mailmap: add more Juergen Borleis' e-mail addresses 2015-06-04 11:03:12 +02:00
COPYING rename U-Boot-v2 project to barebox 2009-12-15 10:18:30 +01:00
Kbuild kbuild: move asm-offsets.h rule to ./Kbuild 2015-01-08 13:55:38 +01:00
Kconfig menuconfig: Show "Barebox", not "Linux" at the top. 2012-04-13 00:10:58 +02:00
MAKEALL Merge branch 'for-next/misc' 2015-02-04 19:09:15 +01:00
Makefile Merge branch 'for-next/doc' 2016-03-11 10:49:43 +01:00
README treewide: drop Doxygen stuff 2014-07-28 07:56:15 +02:00
TODO kbuild: add ./Kbuild file to fix "make clean" 2015-01-05 12:27:21 +01:00

README

Barebox
-------

Barebox is a bootloader that follows the tradition of Das U-Boot, while
adopting modern design ideas from the Linux kernel.


Features
--------

- A POSIX-based file API
  Inside barebox the usual open/close/read/write/lseek functions are used.
  This makes it familiar to everyone who has programmed under UNIX systems.

- Usual shell commands like ls/cd/mkdir/echo/cat,...

- The environment is not a variable store anymore, but a file store. It has
  currently some limitations, of course. The environment is not a real
  read/write filesystem, it is more like a tar archive, or even more like
  an ar archive, because it cannot handle directories. The saveenv command
  saves the files under a certain directory (by default /env) in persistent
  storage (by default /dev/env0). There is a counterpart called loadenv, too.

- filesystem support
  The loader starts up with mounting a ramdisk on /. Then a devfs is mounted
  on /dev allowing the user (or shell commands) to access devices. Apart from
  these two filesystems there is currently one filesystem ported: cramfs. One
  can mount it with the usual mount command.

- device/driver model
  Devices are no longer described by defines in the config file. Instead
  there are devices which can be registered in the board .c file or
  dynamically allocated. Drivers will match upon the devices automatically.

- clocksource support
  Timekeeping has been simplified by the use of the Linux clocksource API.
  Only one function is needed for a new board, no [gs]et_timer[masked]() or
  reset_timer[masked]() functions.

- Kconfig and Kernel build system
  Only targets which are really needed get recompiled. Parallel builds are
  no problem anymore. This also removes the need for many many ifdefs in
  the code.

- simulation target
  barebox can be compiled to run under Linux. While this is rather useless
  in real world this is a great debugging and development aid. New features
  can be easily developed and tested on long train journeys and started
  under gdb. There is a console driver for Linux which emulates a serial
  device and a TAP-based Ethernet driver. Linux files can be mapped to
  devices under barebox to emulate storage devices.

- device parameter support
  Each device can have a unlimited number of parameters. They can be accessed
  on the command line with <devid>.<param>="...", for example
  'eth0.ip=192.168.0.7' or 'echo $eth0.ip'

- initcalls
  hooks in the startup process can be achieved with *_initcall() directives
  in each file.

- getopt
  There is a small getopt implementation. Some commands got really
  complicated (both in code and in usage) due to the fact that U-Boot
  allowed only positional parameters.

- editor
  Scripts can be edited with a small editor. This editor has no features
  except the ones really needed: moving the cursor and typing characters.


Building Barebox
----------------

Barebox uses the Linux kernel's build system. It consists of two parts:
the Makefile infrastructure (kbuild), plus a configuration system
(kconfig). So building barebox is very similar to building the Linux
kernel.

For the examples below, we use the User Mode barebox implementation, which
is a port of barebox to the Linux userspace. This makes it possible to
test drive the code without having real hardware. So for this test
scenario, ARCH=sandbox is the valid architecture selection. This currently
only works on ia32 hosts and partly on x86-64.

Selection of the architecture and the cross compiler can be done by using
the environment variables ARCH and CROSS_COMPILE.

In order to configure the various aspects of barebox, start the barebox
configuration system:

  # make menuconfig

This command starts a menu box and lets you select all the different
options available for your architecture. Once the configuration was
finished (you can simulate this by using the standard demo config file
with 'make sandbox_defconfig'), there is a .config file in the toplevel
directory of the source code.

Once barebox is configured, we can start the compilation

  # make

If everything goes well, the result is a file called barebox:

  # ls -l barebox
    -rwxr-xr-x 1 rsc ptx 114073 Jun 26 22:34 barebox

Barebox usually needs an environment for storing the configuration data.
You can generate an environment using the example environment contained
in board/sandbox/env:

  # ./scripts/bareboxenv -s -p 0x10000 arch/sandbox/board/env env.bin

To get some files to play with you can generate a cramfs image:
  # mkcramfs somedir/ cramfs.bin

The barebox image is a normal Linux executable, so it can be started
just like every other program:

  # ./barebox -e env.bin -i cramfs.bin

  barebox 2.0.0-trunk (Jun 26 2007 - 22:34:38)

  loading environment from /dev/env0
  barebox> /

Specifying -[ie] <file> tells barebox to map the file as a device
under /dev. Files given with '-e' will appear as /dev/env[n]. Files
given with '-i' will appear as /dev/fd[n].

If barebox finds a valid configuration sector on /dev/env0 it will
load it to /env. It then executes /env/init if it exists. If you have
loaded the example environment barebox will show you a menu asking for
your settings.

If you have started barebox as root you will find a new tap device on your
host which you can configure using ifconfig. Once you configured bareboxs
network settings accordingly you can do a ping or tftpboot.

If you have mapped a cramfs image try mounting it with

  # mkdir /cram
  # mount /dev/fd0 cramfs /cram

Memory can be examined as usual using md/mw commands. They both understand
the -f <file> option to tell the commands that they should work on the
specified files instead of /dev/mem which holds the complete address space.
Note that if you call 'md /dev/fd0' (without -f) barebox will segfault on
the host, because it will interpret /dev/fd0 as a number.


Directory Layout
----------------

Most of the directory layout is based upon the Linux Kernel:

arch/*/               -> contains architecture specific parts
arch/*/mach-*/        -> SoC specific code

drivers/serial        -> drivers
drivers/net
drivers/...

include/asm-*         -> architecture specific includes
include/asm-*/arch-*  -> SoC specific includes

fs/                   -> filesystem support and filesystem drivers

lib/                  -> generic library functions (getopt, readline and the
                         like)

common/               -> common stuff

commands/             -> many things previously in common/cmd_*, one command
			 per file

net/                  -> Networking stuff

scripts/              -> Kconfig system

Documentation/        -> Sphinx generated documentation


Release Strategy
----------------

Barebox is developed with git. From time to time, tarball releases are
branched from the repository and released on the project web site. Here
are the release rules:

- Releases follow a time based scheme:

  barebox-xxxx.yy.z.tar.bz2
          ^^^^ ^^ ^----------- Bugfix Number, starting at 0
            \   \------------- Month
             \---------------- Year

  Example: barebox-2009.12.0.tar.bz2

- Releases are made around the beginning of the month. As we are aiming
  for monthly releases, development is considered to be a continuous
  process. If you find bugs in one release, you have the chance to get
  patches in on a very short time scale.

- Usually, there are no bugfix releases, so z=0. If there is a need
  to make a bugfix release, z is the right place to increment.

- If there may be a reason for pre releases, they are called

  barebox-xxxx.yy.z-pren.tar.bz
                       ^------ Number of prerelease, starting with 1

  Example: barebox-2009.12.0-pre1.tar.bz2

  We think that there is no need for pre releases, but if it's ever
  necessary, this is the scheme we follow.

- Only the monthly releases are archived on the web site. The tarballs
  are located in http://www.barebox.org/download/ and this location
  does never change, in order to make life easier for distribution
  people.