\section{\label{ref:Rockboxinflash}Rockbox in flash}
\fixme{These instructions are outdated!!. This section is a copy of the wikipage FlashingRockbox revision r.1.19.}

\warn{Flashing Rockbox is optional. It is not required for using Rockbox on your 
  \playername. Please read the whole section thoroughly before flashing.
}

\subsection{Introduction}
Flashing in the sense used here and elsewhere in regard to Rockbox means 
reprogramming the flash memory of the \playerman\ unit.

When you bought your \playerman, it came with the \playerman\ firmware flashed.
Now, you can add Rockbox to the built-in software.

\subsection{Terminology}
\begin{description}
\item[Firmware: ] The flash ROM content as a whole.
\item[Image: ] Means one operating software started from there
\end{description}

By reprogramming the firmware, we can boot much faster. \playerman\ has an 
unnecessary slow boot loader, versus the boot time for Rockbox is much faster 
than the disk spinup, in fact it has to wait for the disk. Your boot time will 
be as quick as a disk spinup (e.g. 4 seconds from powerup until resuming 
playback).

\subsection{Method}

The replaced firmware will host a bootloader and 2 images. This is possible by 
compression. The first is the \emph{permanent} backup, not to be changed any 
more.The second is the default one to be started, the first is only used when 
you hold the \opt{recorder,recorderv2fm}{\ButtonFOne}\opt{ondio}{\ButtonLeft}\opt{player}{\ButtonLeft} -key during start. Like supplied here, the first image 
is the original Archos firmware, the second is empty, left for you to program 
and update. It can contain anything you like. If you prefer, you can program 
the Archos firmware to there, too.

\note{For now, the binary contained in the brand new player flash package does 
contain rockbox built from current cvs in the second image slot. This is to 
lower the risk of flashing (at least one of the images will hopefully work) in 
case you don't program a second image yourself in the first step. Of course the
second image can be replaced like with the other models.}

There are two programming tools supplied:

\begin{itemize}
\item The first one is called \fname{firmware\_flash.rock} and is used to 
  program the whole flash with a new content. You can also use it to revert 
  back to the original firmware you've hopefully backup-ed. In the ideal case, 
  you'll need this tool only once. You can view this as "formatting" the flash 
  with the desired image structure.
\item The second one is called \fname{rockbox\_flash.rock} and is used to 
  reprogram only the second image. It won't touch any other byte, should be 
  safe to fool around with. If the programmed firmware is inoperational, you 
  can still use the \opt{recorder,recorderv2fm}{\ButtonFOne}\opt{ondio}{\ButtonLeft}\opt{player}{\ButtonLeft} start with the Archos firmware and Rockbox booted
  from disk to try better.
\end{itemize}

The non-user tools are in the \fname{flash} subdirectory of the cvs source 
files. There's an authoring tool which composed the firmware file with the 
bootloader and the 2 images. The bootloader project, a firmware extraction 
tool, the plugin sources, and the tools for the UART boot feature: a monitor 
program for the box and a PC tool to drive it. Feel free to review the sources 
for all of it, but be careful when fooling around with powerful toys!

\subsection{Risks}
Well, is it dangerous? Yes, certainly, like programming a mainboard 
\emph{BIOS}, \emph{CD/DVD} drive firmware, mobile phone, etc. If the power 
fails, your chip breaks while programming or most of all the programming 
software malfunctions, you'll have a dead box. We take no responsibility of any
kind, you do that at your own risk. However, we tried as carefully as possible 
to bulletproof this code. The new firmware file is completely read before it 
starts programming, there are a lot of sanity checks. If any fails, it will not
program. Before releasing this, we have checked the flow with exactly these 
files supplied here, starting from the original firmware in flash. It worked 
reliably, there's no reason why such low level code should behave different on 
your box.

\opt{player}{
  \warn{The risk is slightly higher for player flashing, because:
    \begin{itemize}
      \item This is brand new
      \item It could not be tested with all hardware versions.
    \end{itemize}
    Refer to this e-mail:
    \url{http://www.rockbox.org/mail/archive/rockbox-archive-2004-12/0245.shtml}
  }
}

There's one ultimate safety net to bring back boxes with even completely 
garbled flash content: the \emph{UART} boot mod, which in turn requires the 
serial mod. It can bring the dead back to life, with that it's possible to 
reflash independently from the outside, even if the flash is completely erased.
It has been used that during development, else Rockbox in flash wouldn't have 
been possible. Extensive development effort went into the exploitation of the 
UART boot mod. Mechanically adept users with good soldering skills can easily 
perform these mods. Others may feel uncomfortable using the first tool 
(\fname{firmware\_flash.rock}) for reflashing the firmware.


To comfort you a bit again: If you are starting with a known-good image, you 
are unlikely to experience problems. The flash tools have been stable for quite
a while. Several users have used them extensively, even flashing while playing!
Although it worked, it's not the recommended method.

About the safety of operation: Since we have dual boot, you're not giving up 
the Archos firmware. It's still there when you hold
\opt{recorder,recorderv2fm}{\ButtonFOne}\opt{ondio}{\ButtonLeft}\opt{player}{\ButtonLeft} during startup. So even if Rockbox from flash is not 100\% stable for
everyone, you can still use the box, reflash the second image with an updated 
Rockbox copy, etc.

The flash chip being used by Archos is specified for 100,000 cycles, so you don't need to worry about that wearing out.

\subsection{Requirements}
You need two things:
\begin{itemize}
\item The first is a \playername. Be sure you're using the correct package, 
  they are different!
\item Second, you need an in-circuit programmable flash. \opt{recorder,recorderv2fm,player}{The older chips are not flashable.}\opt{ondio}{This should always 
  be flashable on Ondios, because Archos does itself provide flash updates for 
  these.} You can find out via Rockbox (\setting{Info $\rightarrow$ Debug $\rightarrow$ Hardware Info}). If the flash info gives you question marks (Flash M=?? D=??), 
  you're out of luck. The only chance then is to solder in the right chip 
  (SST39VF020), at best with the firmware already in. If the chip is blank, 
  you'll need the UART boot mod as well.
\end{itemize}

\subsection{Flashing procedure}
Short explanation: copy the \fname{firmware\_*.bin} files for your model from the
distribution to the root directory of your \dap, then run the 
\fname{firmware\_flash.rock} plugin.
Long version, step by step procedure:
\begin{enumerate}
\item Completely install the Rockbox version you want to have in flash, from a 
  full \fname{.zip} distribution, including all the plugins, etc.
\item Back up the current firmware, using the first option of the debug menu 
  (\setting{Info $\rightarrow$ Debug $\rightarrow$ Dump ROM Contents}).
  This creates 2 files in the root directory, which you may not immediately see 
  in the Rockbox browser. The 256kB-sized \fname{internal\_rom\_2000000-203FFFF.bin} one is your present firmware. Back  both up to your PC. You will need them if 
  you want to restore the flash contents.
\item Download the correct package for you model. Copy one or two files of it to 
  your box: \fname{firmware\_*.bin} (name depends on your model) into the root 
  directory (the initial firmware for your model, with the bootloader and the 
  Archos image). There now is also a \_norom variant, copy both, the plugin will 
  decide which one is required for your box.
\item Enter the debug menu and select the hardware info screen. Check your flash 
  IDs (bottom line), and please make a note about your \opt{recorder,recorderv2fm,ondio}{hardware mask value}\opt{player}{ROM version}. The latter is just for our 
  curiosity, not needed for the flow. If the flash info shows question  marks, 
  you can stop here, sorry.
\item Use the \opt{recorder,recorderv2fm}{\ButtonFTwo\ settings or }the menu (\setting{General settings $\rightarrow$ File view $\rightarrow$ Show files}) to 
  configure seeing all files within the browser.
\item Connect the charger and make sure your batteries are also in good shape. 
  That's just for security reasons, it's not that flashing needs more power.
\item Run the \fname{firmware\_flash.rock} plugin. It again tells you about your 
  flash and the file it's gonna program. After \opt{recorder,recorderv2fm}{\ButtonFOne}\opt{ondio}{\ButtonLeft}\opt{player}{\ButtonLeft} it checks the file. Your 
  hardware mask value will be kept, it won't overwrite it. Hitting \opt{recorder,recorderv2fm}{\ButtonFTwo}\opt{ondio}{\ButtonUp}\opt{player}{\ButtonOn} gives you 
  a big warning. If we still didn't manage to scare you off, you can hit\opt{recorder,recorderv2fm}{\ButtonFThree}\opt{ondio}{\ButtonRight}\opt{player}{\ButtonRight} to actually program and verify. The programming takes just a few seconds. If 
  the sanity check fails, you have the wrong kind of boot ROM and are out of luck
  by now, sorry.
\item In the unlikely event that the programming should give you any error, don't
  switch off the box! Otherwise you'll have seen it working for the last time. 
  While Rockbox is still in DRAM and operational, we could upgrade the plugin via
  USB and try again. If you switch it off, it's gone.
\end{enumerate}

\nopt{player}{
Now the initial procedure is done. Since the second half of the flash is still 
empty, there is ``just'' the Archos image starting when you reboot now. Not much 
has changed yet. The Archos software starts a bit quicker than usual, then loads 
Rockbox from disk. The fun really starts when you add Rockbox to the flash, as 
described in the next section.
}

\note{You may delete the \fname{.bin} files now.}

\subsection{Bringing in a Rockbox build}
Short version: very easy, just play an \fname{.ucl} file like 
\fname{rockbox.ucl} from a release or build:

\begin{itemize}
\item Make sure you are running the same version that you are trying to flash: 
  play the \fname{ajbrec.ajz} file.
\item  Enter the \fname{.rockbox} directory in the file browser (you might need 
  to set the \setting{File View} option to \setting{All Files}).
\item Play the \fname{rockbox.ucl} file (or \fname{rombox.ucl} if you want to 
  flash ROMBox)
\end{itemize}

Long version:

The second image is the working copy, the \fname{rockbox\_flash.rock} plugin from
this package reprograms it. The plugins needs to be consistant with the Rockbox 
plugin API version, otherwise it will detect mismatch and won't run.

It requires an exotic input, a UCL-compressed image, because that's the internal 
format. UCL is a nice open-source compression library. The decompression is very 
fast and less than a page of C-code. The efficiency is even better than Zip with 
maximum compression, reduces file size to about 58\% of the original size. For 
details on UCL, see \url{http://www.oberhumer.com/opensource/ucl/}.

Rockbox developers using Linux will have to download it from there and compile 
it. For Win32 and Cygwin the executables are next to the packages. The sample 
program from that download is called \fname{uclpack}. We'll use that to compress 
\fname{rockbox.bin} which is the result of the compilation. This is a part of the
build process meanwhile. If you compile Rockbox yourself, you should copy 
\fname{uclpack} to a directory which is in the path, we recommend placing it in 
the same dir as SH compiler.


Here are the steps:

\begin{enumerate}
\item Normally, you'll simply download a \fname{.zip} distribution. Copy all the 
  content to the USB drive, replacing the old.
\item Force a disk boot by holding \opt{recorder,recorderv2fm}{\ButtonFOne}\opt{ondio}{\ButtonLeft}\opt{player}{\ButtonLeft} during power-up, or at least rolo into
  the new Rockbox version by \emph{Playing} the \fname{ajbrec.ajz}/fname{archos.mod} file. This may not always be necessary, but it's better to first run the 
  version you're about to flash. It is required if you are currently running 
  RomBox.
\item Just \emph{play} the \fname{.ucl} file in the \fname{.rockbox} directory, 
  this will kick off the \fname{rockbox\_flash.rock} plugin. It's a bit similar 
  to the other one, but it's made different to make the user aware. It will check
  the file, available size, etc. With \opt{recorder,recorderv2fm}{\ButtonFTwo}\opt{ondio}{\ButtonUp}\opt{player}{\ButtonOn} it's being programmed. No need for 
  warning this time. If it goes wrong, you'll still have the permanent image.
\item When done, you can restart the box and hopefully your new Rockbox image.
\end{enumerate}

You may find two \fname{.ucl} files in the \fname{.rockbox} folder. The 
classical, compressed one is \fname{rockbox.ucl}. If your model has enough flash 
space left, there may be an additional \fname{rombox.ucl}, which is uncompressed 
and can run directly from flash ROM, saving some RAM. The second way is the newer
and now preferred one. Use this if available.

If you like or have to, you can also flash the Archos image as the second one. 
E.g. in case Rockbox from flash doesn't work for you. This way you keep the dual 
bootloader and you can easily try different later. The \fname{.ucl} of the Archos
firmware is included in the package.

\subsection{Restoring the original firmware}
If you'd like to revert to the original firmware, you can do like you did when 
you flashed Rockbox for the first time. You simply use the backup files you saved
when flashing Rockbox for the first time and rename \fname{internal\_rom\_2000000-203FFFF.bin} to \fname{firmware\_*.bin} (name varies per model, use the filename that \fname{firmware\_flash.rock} asks for) and put it in the root.

\subsection{Known issues and limitations}
Rockbox has a charging screen, but it is not 100\% perfect. You'll get it when 
the unit is off and you plug in the charger. The Rockbox charging algorithm is 
first measuring the battery voltage for about 40 seconds, after that it only 
starts charging when the capacity is below 85\%. 
\opt{recorder,recorderv2fm}{You can use the Archos charging (which always tops off) by holding \ButtonFOne\  while plugging in.}\opt{recorderv2fm}{Some FM users reported charging problems even with  \ButtonFOne, they had to revert to the original flash content.}

If the plugin API is changed, new builds may render the plugins incompatible. 
When updating, make sure you grab those too, and ROLO or \opt{recorder,recorderv2fm}{\ButtonFOne}\opt{ondio}{\ButtonLeft}\opt{player}{\ButtonLeft} boot into the 
new version before flashing it.

There are two variants of how the boxes starts, therefore the normal and the 
\_norom firmware files. The vast majority of the \daps\ all have the same boot 
ROM content, differentiation comes later by flash content. Rockbox identifies 
this boot ROM with a CRC value of 0x222F in the hardware info screen. \opt{recorder,recorderv2fm}{Some recorders have the boot ROM disabled (it might be unprogrammed) and start directly from a flash mirror at address zero. They need the new 
  \_norom firmware that has a slightly different bootloader.}
Without a boot ROM  there is no UART boot safety net. To compensate for that as 
much as possible the MiniMon monitor is included, it starts with \opt{recorder,recorderv2fm}{\ButtonFThree+\ButtonOn}\opt{ondio}{\ButtonRight+\ButtonOff}\opt{player}{\ButtonRight+\ButtonOn}.
Using that the box can be reprogrammed via serial if the first 2000 bytes of the
flash are OK.

\subsection{Download the new flash content file to your box}
\fixme{These links are not valid. Refer to the wikipage BootBox for further 
  instructions}
Jens Arnold hosts flash content for download. Use the following url:
\opt{player}{\url{http://www.jens-arnold.net/Rockbox/flash\_player.zip}}
\opt{recorder}{\url{http://www.jens-arnold.net/Rockbox/flash\_rec.zip}}
\opt{recorderv2fm}{\url{http://www.jens-arnold.net/Rockbox/flash\_fm.zip},
  \url{http://www.jens-arnold.net/Rockbox/flash\_v2.zip}}
\opt{ondiofm}{\url{http://www.jens-arnold.net/Rockbox/flash\_ondiofm.zip}}
\opt{ondiosp}{\url{http://www.jens-arnold.net/Rockbox/flash\_ondiosp.zip}}
