﻿<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE guide SYSTEM "/dtd/guide.dtd">

<guide link="" disclaimer="draft">

<title>Gentoo on the Nvidia Tegra 250 Developer Kit</title>
<author title="Author">
  <mail link="armin76"/>
</author>

<abstract>
This guide shows you how to install Gentoo on the Nvidia Tegra 250 Developer Kit.
</abstract>

<!-- The content of this document is licensed under the CC-BY-SA license -->
<!-- See http://creativecommons.org/licenses/by-sa/2.5 -->
<license/>

<version>1</version>
<date>2011-06-13</date>

<chapter>
<title>Overview</title>
<section>
<body>
<p>
Installing Gentoo in the Nvidia Tegra 250 is pretty simple if you're already a Gentoo user.
You can either use a sufficiently sized SD card (>=2GB for ease) or an USB HDD. If you
are familar with the Gentoo Linux installation process, there is not much
different here.
</p>
</body>
</section>
</chapter>

<chapter>
<title>Requirements</title>
<section>
<body>

<p>
To be able to install Gentoo, you'll need the following:
</p>

<ul>
  <li>An x86/amd64 based PC with Linux on it</li>
  <li>A Nvidia Tegra 250 Developer Kit</li>
  <li>The USB -> USB mini type B cable the board comes with</li>
<!--<li>One SD card/One USB HDD</li> # No SD card yet -->
  <li>One USB HDD</li>
  <li>A network connection</li>
</ul>

</body>
</section>
</chapter>
<chapter>
<title>Preparing to install your Nvidia Tegra</title>
<section>
<title>Overview</title>
<body>
<p>
Before we start the installation process, we need to build a kernel for the Nvidia Tegra
and the utility to flash it to our board on our computer.
</p>

<p>
The Nvidia Tegra comes without any Operating System on it. At the moment it uses
the fastboot method for flashing a kernel in case of Linux(Windows CE/Android is
another story). U-Boot works but for now we'll use the official method.
</p>
</body>
</section>

<section>
<title>Obtaining/Building a kernel</title>
<body>
<p>
For booting the Nvidia Tegra we need a kernel. In this document we'll use the kernel provided
by Nvidia which is available on Nvidia's GIT server. For doing so
we need <i>dev-vcs/git</i> on our system.
</p>
<note>Kernel.org's kernel still doesn't support some hardware on the board as of 2011-06-18</note>
<pre caption="Get the kernel source">
<comment>(Emerge dev-vcs/git if you don't have it already)</comment>
# <i>emerge dev-vcs/git</i>

<comment>(Clone Nvidia Tegra kernel repository, may take a while)</comment>
# <i>git clone git://nv-tegra.nvidia.com/linux-2.6.git</i>

<comment>(Once cloned, change to the new directory created)</comment>
# <i>cd linux-2.6</i>

<comment>(The tegra-10.9.9 tag is known to work)</comment>
# <i>git checkout tegra-10.9.9</i>
</pre>
<p>
Now that we have the kernel source, we can build it. However since the Nvidia Tegra is an ARM
device, we need to crosscompile the kernel to ARM. For doing so, on Gentoo we use <i>sys-devel/crossdev</i>,
which provides us an easy way to create a cross-compiler. On other Linux distros, you can use
the CodeSourcery toolchain.
</p>
<pre caption="Creating a cross-compiler">
<comment>(Emerge sys-devel/crossdev first, if you don't have it already)</comment>
# <i>emerge -v crossdev</i>

<comment>(Create a crosscompiler for the armv7a-unknown-linux-gnueabi CHOST)</comment>
# <i>crossdev -S armv7a-unkown-linux-gnueabi</i>
</pre>

<p>
Once we have our crosscompiler, we can crosscompile it finally. We'll use the default kernel config
since it has all we need.
</p>

<pre caption="Cross-compiling the kernel">
<comment>(We use the default config for this board)</comment>
# <i>make ARCH=arm CROSS_COMPILE="armv7a-unknown-linux-gnueabi-" tegra_harmony_gnu_linux_defconfig</i>

<comment>(Replace 9 with the number of cores you have on your computer)</comment>
# <i>make -j9 ARCH=arm CROSS_COMPILE="armv7a-unknown-linux-gnueabi-" zImage</i>
</pre>

<p>
Once it gets built, we'll now have a kernel image on arch/arm/boot/zImage.
</p>

</body>
</section>

<section>
<title>Obtaining and building the fastboot loader</title>
<body>
<p>
The bootloader we'll use is the one that gets used for Android.
This bootloader, fastboot, is available on the Android SDK</p>
<note>If the following download is not available, check on
<uri link="http://developer.nvidia.com/node/19086">Nvidia's Tegra250 Downloads page</uri>
for the latest version of the Android for Linux download</note>
<pre caption="Download and unpack Nvidia's Android SDK for Linux">
# <i>wget http://developer.download.nvidia.com/tegra/files/os/tegra_froyo_20110207.run.gz</i>

<comment>(After having downloaded the SDK, we decompress it)</comment>
# <i>gzip -d tegra_froyo_20110207.run.gz</i>

<comment>(We set the execution mode on the resulting binary)</comment>
# <i>chmod +x tegra_froyo_20110207.run</i>

<comment>(We execute the binary)</comment>
# <i>./tegra_froyo_20110207.run</i>
</pre>
<p>
After accepting the license, the binary will extract a directory called <i>tegra_froyo_20110207</i>.
In this directory you'll find the fastboot bootloader.
</p>

<p>
<b>You also need to copy the previously built zImage into that directory</b>
</p>

<p>We'll enter into the new directory and download the following file
which contains the partitions that will be created on the board's NAND.
</p>

<pre caption="Getting the NAND partition config file">
# <i>cd tegra_froyo_20110207</i>

# <i>wget http://dev.gentoo.org/~armin76/arm/tegra2/linux_fastboot_nand.cfg</i>
</pre>


</body>
</section>

<section>
<title>Compile the fastboot utility on your PC</title>
<body>
<p>
For flashing a kernel into the board, we need the fastboot utility
on our PC. The flashing is done through the miniUSB port the board has.
</p>

<note>You can download a fastboot binary instead from <uri link="http://developer.htc.com/adp.html">
HTC's webpage</uri></note>

<p>
For building such tool, we need, unfortunately, java.
</p>

<pre caption="Emerging required dependencies">
# <i>emerge -v =sun-jdk-1.5*</i>
</pre>

<p>
Then, we'll checkout the repositories that are needed for fastboot
</p>

<pre caption="Checking out fastboot repositories">
# <i>git clone git://nv-tegra.nvidia.com/android/platform/build.git build</i>
# <i>git clone git://nv-tegra.nvidia.com/android/platform/external/svox.git external/svox</i>
# <i>git clone git://nv-tegra.nvidia.com/android/platform/external/zlib.git external/zlib</i>
# <i>git clone git://nv-tegra.nvidia.com/android/platform/system/core.git system/core</i>

# <i>cd system/core</i>

<comment>(Get the correct version)</comment>
# <i>git checkout tegra-9.12.5</i>
# <i>cd ..</i>
</pre>

<p>
Now, we compile it.
</p>

<pre caption="Compiling fastboot">
# <i>make -f build/core/main.mk fastboot</i>
</pre>

</body>
</section>


<section>
<title>Flashing the bootloader</title>
<body>
<p>
Now we'll format the NAND and flash the bootloader into it.
</p>

<p>Make sure you plug the power cable on the board, then connect the usb cable
to the board and to your PC.</p>

<p>To power on the board, you need to press <b>*BOTH*</b> the <b>Force Recovery</b> and the <b>Power on</b>
buttons until the LEDs power up.
Then execute the following command to flash the bootloader.
</p>

<impo>Remember to copy the previously zImage into the tegra_froyo_20110207 directory!</impo>
<pre caption="Recreating the partitions and flashing the bootloader">
# <i>./nvflash --bct flash.bct --setbct --bl bootloader.bin --configfile linux_fastboot_nand.cfg --odmdata 0x303d8011 --create --go</i>

<comment>If everything went right, you should see the following output on the command line:</comment>
Nvflash started
rcm version 0X20001
System Information:
   chip name: t20
   chip id: 0x20 major: 1 minor: 2
   chip sku: 0x8
   chip uid: 0x1614308744607417
   macrovision: disabled
   hdcp: enabled
   sbk burned: false
   dk burned: false
   boot device: nand
   operating mode: 3
   device config strap: 0
   device config fuse: 0
   sdram config strap: 0

sending file: tegra_250_333MHz_1GB.bct
- 4080/4080 bytes sent
tegra_250_333MHz_1GB.bct sent successfully
odm data: 0x303d8011
downloading bootloader -- load address: 0x108000 entry point: 0x108000
sending file: fastboot.bin
\ 888192/888192 bytes sent
fastboot.bin sent successfully
waiting for bootloader to initialize
bootloader downloaded successfully
setting device: 1 0
creating partition: BCT
creating partition: PT
creating partition: EBT
creating partition: UIP
creating partition: USP
creating partition: SOS
creating partition: LNX
creating partition: MBR
creating partition: UBN
creating partition: MPT
Formatting partition 2 BCT please wait.. done!
Formatting partition 3 PT please wait.. done!
Formatting partition 4 EBT please wait.. done!
Formatting partition 5 UIP please wait.. done!
Formatting partition 6 USP please wait.. done!
Formatting partition 7 SOS please wait.. done!
Formatting partition 8 LNX please wait.. done!
Formatting partition 9 MBR please wait.. done!
Formatting partition 10 UBN please wait.. done!
Formatting partition 11 MPT please wait.. done!
done!
sending file: fastboot.bin
\ 888192/888192 bytes sent
fastboot.bin sent successfully
sending file: zImage
\ 2488384/2488384 bytes sent
zImage sent successfully

<comment>On the Nvidia Tegra board video output you should see:</comment>
"Entering NvFlash recovery mode / Nv3p Server"

"Starting Fastboot USB download protocol"
</pre>

<p>
That means the flashing of the bootloader went correctly, we can now flash the kernel.
</p>

<pre caption="Flashing the kernel">
<comment>(Replace sda1 with the partition of the disk where you will put Gentoo on)
(Replace PATH_TO_zImage with the path where the zImage you build is located)</comment>
# <i>out/host/linux-x86/bin/fastboot -i 0x955 -p sda1 flash:raw boot PATH_TO_zImage</i>

<comment>If everything went right, you should see the following output:
(Note: the bytes may change)</comment>
creating boot image...
creating boot image - 2492416 bytes
sending 'boot' (2434 KB)... OKAY
writing 'boot'... OKAY

</pre>

<p>Remember to save this command(and the binaries), since its the command you'll need to put a new
kernel</p>

<p>That means our Nvidia Tegra is ready to boot, now you can turn it off, and let's focus on the Gentoo
installation on our USB disk</p>
</body>
</section>

</chapter>
<chapter id="install">
<title>Installing Gentoo</title>
<section>
<title>Overview</title>
<body>

<p>
The installation this device is a bit different, as we can't install Gentoo on it
by booting an installation environment. For installing Gentoo(and any other distro, really)
you need to put the USB disk or usb stick on your PC and prepare there the
minimal installation.
</p>

<p>
I'd recommend reading the ARM handbook, for creating the partitions, extracting
the stage, configure the networking, etc...until you're able to create a
minimum filesystem.
</p>

<p>
So please read and follow the <uri link="http://www.gentoo.org/doc/en/handbook/handbook-arm.xml">ARM Handbook</uri> from the beginning, just skip the 10th
chapter, as its irrelevant for our machine. I'll just explain what we have to
do differently. Please read below first so you know when you have to go back to
this document during the installation.
</p>
</body>
</section>
<section>
<title>Stages information</title>
<body>
<p>
Here's some information about the stages.
</p>
<ul>
  <li>Architecture: arm</li>
  <li>Subarchitecture: armv7a</li>
  <li>CHOST: armv7a-unknown-linux-gnueabi</li>
  <li>Profile: default/linux/arm/10.0</li>
</ul>
<p>
We'll be using the new EABI, also called gnueabi, instead of the old ABI. That
is <i>armel</i> on Debian. There's no exact reason for why we should use this
EABI and not the old one, apart that this one is better than the old.
</p>
<p>
Therefore, we need an armv7a-unknown-linux-gnueabi stage3, available under the
releases/arm/autobuilds directory in your <uri link="http://www.gentoo.org/main/en/mirrors2.xml">favorite mirror</uri>
</p>
</body>
</section>

<section>
<title>Setting the default root password</title>
<body>
<p>
This is the most important part of the installation. As without the root 
password we won't be able to login!
</p>

<p>
For setting the password, we need to be able to run <i>passwd</i>. However thats
not possible since our PC can't run ARM binaries. Therefore we need to modify
the file that contains the passwords, <i>/etc/shadow</i> inside the chroot,
 so we can set a default root password.
</p>

<warn>Remember that this paths are relative to the chroot! So if
you extracted the stage3 on <b>/mnt/hdd/</b>, then you should add <b>/mnt/hdd</b> everytime 
i mention a file or directory</warn>
<pre caption="Change the default root password to gentoo">
<comment>(This would be <b>/mnt/hdd/etc/shadow</b> in the example above)</comment>
# <i>nano -w /etc/shadow</i>

<comment>(Replace the first line with the following line)</comment>
root:$6$I9Q9AyTL$Z76H7wD8mT9JAyrp/vaYyFwyA5wRVN0tze8pvM.MqScC7BBm2PU7pLL0h5nSxueqUpYAlZTox4Ag2Dp5vchjJ0:14698:0:::::
</pre>

<p>
After saving the file, that means that on the installation, the password will be
<i>gentoo</i>. You can change it once you boot your Nvidia Tegra board.
</p>

</body>
</section>
<section>
<title>Enabling SSH access(optional)</title>
<body>
<p>
We can add <i>sshd</i> to the startup of our system so we can access the Nvidia Tegra
board using ssh.
</p>
<pre caption="Adding sshd to the startup">
# <i>rc-update add sshd default</i>
</pre>
</body>
</section>
<section>
<title>Enabling serial console access(optional)</title>
<body>
<p>
By default the ttyS0 port is configured at 9600bps. However,almost all the
ARM devices run the serial port at 115200bps. So this should be added 
to the <i>/etc/inittab</i> file:
</p>
<pre caption="Configuring serial console">
# <i>nano -w /etc/inittab</i>

<comment>(Replace 9600 with 115200 on the ttyS0 line)</comment>
s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100
</pre>
</body>
</section>
<section>
<title>Finishing the installation</title>
<body>
<p>
This is pretty much all the installation, read all the recommendations of the
handbook, and remember to change the root password and/or create some users.
</p>
</body>
</section>
</chapter>

<chapter>
<title>Booting to our new system</title>
<section>
<body>
<p>
Once you've finished the installation of Gentoo into your Nvidia Tegra, you can
simply push the reset button. Then it will boot from the external storage device.
</p>
</body>
</section>
</chapter>

<chapter>
<title>References</title>
<section>
<body>
<p>
You may find more documentation about the device itself and Linux-related at
the following links:
</p>
<ul>
	<li><uri link="http://tegradeveloper.nvidia.com/tegra/forum/workaround-run-ubuntu-now">ianr's post on Nvidia Tegra forums about booting Linux</uri></li>
	<li><uri link="http://tegradeveloper.nvidia.com/tegra/forums/tegra-forums/linux-development">Linux development subforum on the Nvidia Tegra forums</uri></li>
    <li><uri link="http://tegradeveloper.nvidia.com/tegra/">Nvidia's Developer Zone for Tegra</uri></li>
</ul>
</body>
</section>
</chapter>

<!--<chapter>
<title>Thanks</title>
<section>
<body>
<p>
Myself!
</p>
</body>
</section>
</chapter>-->
</guide>
