ONLamp.com    
 Published on ONLamp.com (http://www.onlamp.com/)
 See this if you're having trouble printing code examples


Big Scary Daemons Palmtop NetBSD

by Michael W. Lucas
06/05/2003

In my previous article, I discussed building the latest NetBSD-current and installing it on your system. Today, we take things a little further and use NetBSD's cross-building abilities as a tool to install it on a rather unusual platform — specifically, the HP Jornada 728 palmtop, which comes with Windows CE (WinCE) embedded. While HP no longer makes this device, you can pick them up used or on auction sites. The process discussed here uses examples from the Jornada 728, but with some tweaking will work on many other types of hardware. You could even install onto standard PC hardware in this manner, if you wanted to be masochistic about it.

Setup

This method requires that you already have a NetBSD machine that can physically accept the disk device you want to install onto, and that has the same sort of disklabel(8) as the Jornada, which means that you need the same endian hardware. You're probably best off running standard X86 hardware for this type of installation on this type of palmtop. In this example, I use a Toshiba laptop running NetBSD-current 1.6P to install NetBSD-current 1.6P on my Jornada. I'm using a 512MB flash card to run NetBSD, and am using a PCMCIA adapter to attach the flash card to the laptop during the installation phase. If you're using a desktop, you could use a USB-to-Flash adapter.

Running NetBSD on your Jornada will erase all data from the WinCE operating system. That's OK, because you'll never boot back to Windows once you discover the joys of palmtop UNIX, right? Still, you might want the WinCE data some day, so be certain to back up your device before starting this install! Some people who switch boots between WinCE and NetBSD use the backup tool that comes with WinCE to back up their data to the FAT partition on their flash card. If you do this, be sure to use only the backup tool that came with the Jornada, not a newer version.

The Complete FreeBSD

Related Reading

The Complete FreeBSD
Documentation from the Source
By Greg Lehey

NetBSD runs on a whole bunch of hardware that was never intended to run UNIX, so this install isn't really anything special. Each of these unusual platforms has its own unique requirements, which are generally documented on the appropriate port page for that architecture. Check the port page for an instructions document, which in this case is the Using NetBSD/hpcarm page. The directions may be partially obsolete, as code development frequently outpaces the web documentation. Still, it's a good place to start. Also search the mailing list archives for your chosen platform for other people's experiences installing and running NetBSD on that system. Lastly, you'll want to be certain that NetBSD runs on your system — for example, while the Jornada 720 and 728 are well-supported, the Jornada 820 isn't yet.

Bootstrapping

The how-to-use page for hpcarm shows that you need a kernel and a boot loader that runs in the WinCE environment. At this time the page also mentions a root filesystem image, which is not yet available. The only piece you really need to get at this point is the hpcarm boot loader, which is available as a uuencoded file in the NetBSD source tree at /usr/src/sys/arch/hpc/stand/binary/ARM/hpcboot.exe.uu. If you have trouble with uudecode, Google led me to a variety of places from which it could be downloaded. Get the boot loader hpcboot.exe and save it for later. Don't worry about the kernel linked from that page; we'll build our own kernel.

Now go to your existing NetBSD system. We covered the basics of building NetBSD in the last article, including getting the source code and using the build.sh building system. As each platform has its own compiler requirements, make(1), and so on, we need to start by building these tools for the hpcarm architecture. Here, we use the -T flag to specify where to put these tools, and the -m flag to specify the type of machine for which you're building.

# cd /usr/src
# ./build.sh -m hpcarm -T /usr/src/hpcarmtools tools kernel=JORNADA720

This will create a full set of up-to-date hpcarm tools in /usr/src/hpcarmtools, and an hpcarm kernel from the JORNADA720 configuration file in /usr/obj/sys/arch/hpcarm/compile/JORNADA720/netbsd. Obviously, you cannot test-boot your new kernel, at least, not yet; we just have to build the userland and hope it works. As we're building a 1.6P userland on top of a 1.6P kernel, using build.sh to abstract away such petty details as the underlying hardware, we shouldn't have any problems. This is almost identical to how we built the userland in the last article, but be certain to specify the tools directory with -T.


# ./build.sh -m hpcarm -T /usr/src/hpcarmtools -U \
      -D /usr/hpcarmbuild distribution

Preparing Your Disk

While your system churns out a new userland, you can prepare your disk. You need a flash disk or one of those PC-card disks to install NetBSD on. Sadly, you cannot overwrite the Jornada's WinCE image with NetBSD. Instead, you have to use WinCE almost as a BIOS and boot from it. I chose a 512MB Compact Flash card, leaving the PC-card slot free for a network card. (While a palmtop running UNIX would certainly make me the office alpha geek, having a wireless network connection on top of the whole thing would keep anyone from ever trying to supplant me.)

The disk must contain a boot loader and a kernel visible to WinCE, which means that you need a small FAT partition. If you want WinCE to recognize the disk partition, it must lie at the beginning of the disk. 5MB is sufficient, as long as you don't want to have several large kernels hanging around. The rest of your disk will be a UFS partition. You must manually label the disk.

We've looked at disklabels before, but NetBSD is slightly different from either FreeBSD or OpenBSD. In NetBSD, the c partition is the entire NetBSD portion of the disk, while d represents the entire disk. Our FAT partition (traditionally partition e in NetBSD-land) will be inside of partition d, but not inside of partition c.

Inside of our NetBSD area c we need at least one UFS filesystem partition, a. As this is a small disk, we'll make do with a single UFS filesystem; there's no need to break up the small UFS area into /tmp, /usr, and so on. We won't be using this palmtop as a multi-user server, after all! As swapping to flash is a bad idea, we won't include a swap partition. In a future article, we'll get swap on your palmtop without using the flash disk. All of this means that our a partition will exactly overlap the c partition. Put your flash card into the adapter, put it in your existing NetBSD system, and boot. On my laptop, the flash device shows up as wd1. Make the appropriate substitutions for how your flash card appears.

Let's interactively edit the disklabel. Become root and run disklabel(8).

# disklabel -i -I wd1
partition>

To see all the possible options, enter a question mark. One important option is P, meaning "print the existing partition table."

partition> P
8 partitions:
#        size    offset     fstype  [fsize bsize cpg/sgs]
 d:   1000944         0     unused      0     0         # (Cyl.    0 - 992)
partition>

disklabel(8) shows that we have a single large partition, d, on a blank flash disk. This is exactly where we want to start. As we need the FAT partition e to be first on the disk, create it first. (We could do a bit of math to create our UFS partition first, but that bears a suspicious resemblance to actual work.) Enter e to edit partition e.

partition> e
Filesystem type [?] [unused]: MSDOS
Start offset [0c, 0s, 0M]: 
Partition size ('$' for all remaining) [0c, 0s, 0M]: 5MB
 e:     10240         0      MSDOS                      # (Cyl.    0 - 10*)
partition>

This is a FAT partition that shows up as MSDOS in a disklabel. As we want this to start at the beginning of the disk, you can take the default start offset of zero. Enter 5MB for the partition size.

Now, grab everything else for our c partition. As the e partition has a size of 10240 sectors, numbered from 0 through 10239, our new partition begins on sector 10240. Remember, while c is used for the entire NetBSD area of the disk, it has a type of unused. Partitions within the c area have a type of 4.2BSD.

partition> c
Filesystem type [?] [unused]: 
Start offset [0c, 0s, 0M]: 10240
Partition size ('$' for all remaining) [0c, 0s, 0M]: $
 c:    990704     10240     unused      0     0         # (Cyl.   10*- 992)
partition>

Now, add an a partition that happens to fill the c partition. The only difference here is how we set the filesystem type.

partition> a
Filesystem type [?] [unused]: 4.2BSD
Start offset [0c, 0s, 0M]: 10240
Partition size ('$' for all remaining) [0c, 0s, 0M]: $
 a:    990704     10240     4.2BSD      0     0     0   # (Cyl.   10*- 992)
partition>

Your flash disk should now have a DOS partition, a 4.2BSD partition, and two unused partitions.

partition> P
8 partitions:
#        size    offset     fstype  [fsize bsize cpg/sgs]
 a:    990704     10240     4.2BSD      0     0     0   # (Cyl.   10*- 992)
 c:    990704     10240     unused      0     0         # (Cyl.   10*- 992)
 d:   1000944         0     unused      0     0         # (Cyl.    0 - 992)
 e:     10240         0      MSDOS                      # (Cyl.    0 - 10*)
partition>

To make your changes permanent, write the label.

partition> W
Label disk [n]? y
Erase the previous contents of the disk? [n]: y
Label written
partition> Q
#

We're close, but not quite there. Even on a flash disk, you need a place to put your fdisk information. This goes in the first cylinder on the disk, or the first 63 sectors. Even though a flash disk has no cylinders, you need to provide space for this record, or your first newfs will obliterate your disklabel.

Looking at the earlier example, doesn't it seem that you could have set this during the initial disklabel? You would think so, but the version of WinCE on my particular Jornada does not like the DOS partition created in that way. When I left space for the disklabel during the initial labeling process, WinCE offered to format the flash disk for me. Editing the disklabel by hand is not as scary as you might think, especially when you just want to make a single minor change.

# disklabel -e /dev/wd1

This will pop up the flash card's disklabel in your editor. First, change the offset on the first partition to 63 sectors, so that your disklabel will not be overwritten. As we don't want the partitions with filesystems to overlap, you must remove 63 sectors from the total size of partition e, reducing it from 10240 to 10177 sectors. The necessary change is shown in bold below.

8 partitions:
#        size    offset     fstype  [fsize bsize cpg/sgs]
 a:    990704     10240     4.2BSD   1024  8192    87   # (Cyl.   10*- 992)
 c:    990704     10240     unused      0     0         # (Cyl.   10*- 992)
 d:   1000944         0     unused      0     0         # (Cyl.    0 - 992)
 e:     10177        63      MSDOS                      # (Cyl.    0*- 10*)

When you save your work and exit, disklabel(8) will make the necessary changes to the disklabel on the flash card.

While you have two usable partitions now, /dev/wd1a and /dev/wd1e, neither can store any data without having a filesystem first. Remember, when formatting NetBSD filesystems, use the raw device name.

# newfs_msdos /dev/rwd1e
/dev/rwd1e: 10192 sectors in 1274 FAT12 clusters (4096 bytes/cluster)
bps=512 spc=8 res=1 nft=2 rde=512 sec=10240 mid=0xf8 spf=4 spt=63 hds=16 hid=63
#

If you didn't give an offset of 63 for the e partition, at this point your disklabel will disappear. Nifty, eh? Next, create a UFS filesystem for your main NetBSD install.

# newfs /dev/wd1a
Warning: 160 sector(s) in last cylinder unallocated
/dev/wd1a:      990704 sectors in 983 cylinders of 16 tracks, 63 sectors
        483.7MB in 12 cyl groups (87 c/g, 42.82MB/g, 10048 i/g)
super-block backups (for fsck -b #) at:
     32,  87792, 175552, 263312, 351072, 438832, 526592, 614352, 702112,
 789872, 877632, 965392,
#

At this point, you should have a blank flash disk with two partitions. Before proceeding, I suggest inserting the flash card into your Jornada to confirm that the FAT partition is readable under WinCE and that the UFS partition doesn't make it choke. If WinCE complains, your disklabel is unsuitable. Try to prepare the disk in a slightly different way. Perhaps under your version of WinCE, you must leave space for the disklabel when initially creating the disk. Maybe you must let WinCE format the DOS partition, and then disklabel the NetBSD partition. Perhaps you must use fdisk(8). This depends almost entirely upon the idiosyncrasies of the version of Microsoft's palmtop operating system your device is running. Once you can swap the flash card easily between NetBSD and WinCE, you're ready to proceed.

Now that the disk has filesystems recognizable by both the palmtop and by NetBSD, you can mount them in NetBSD. As we're only doing an install, and the risk of data corruption is low, we can mount the UFS partition async to make it slightly faster. To save writes on the flash disk, we're going to mount it noatime and nodevmtime as well. It would be nice to use similar features on the FAT partition, but that's not an option. Instead, we'll settle for using the -l option, which generates long and mixed-case filenames. We will want the FAT partition to be visible when we're running NetBSD, so mount it under the soon-to-be root partition.

# mkdir /mnt/jornada
# mount /dev/wd1a -o noatime,nodevmtime /mnt/jornada
# mkdir /mnt/jornada/dos
# mount_msdos -o -l /dev/wd1e /mnt/jornada/dos
# 

Installing NetBSD

By now, your system should have finished building the hpcarm binaries. Let's return to our old friend, build.sh, to install them.

# cd /usr/src
# ./build.sh -m hpcarm -T /usr/src/hpcarmtools -U \
	-D /usr/hpcarmbuild install=/mnt/jornada

postinstall will scream bloody murder because most of /etc isn't there, as well as such trivial bits such as /dev/MAKEDEV and friends. I suggest that you follow postinstall's advice, and let it do part of your work by running the commands it suggests. Then populate your new /etc directory on our flash disk from the /etc directory built by build.sh. This will overwrite some (but not all) of the work done by postinstall, and is the simplest way to get everything you need onto the disk.

# cd /usr/hpcarmbuild/etc
# pax -rw -pe . /mnt/jornada/etc/
#

Your main NetBSD install is finished — if you can boot it! This is where your FAT partition comes in. Get the boot loader you found earlier, hpcboot.exe, and copy it onto your FAT filesystem. The "How To Use" page also lists a kernel file for use on the FAT filesystem. Do not use that kernel file, as it is actually a zipped binary that including the installer and some very rudimentary commands that you could use to bootstrap the system from a blank flash disk. Instead, put the kernel you built with build.sh on your FAT partition.

# cp hpcboot.exe /mnt/jornada/dos
# cp /usr/obj/sys/arch/hpcarm/compile/JORNADA720/netbsd \ 
	/mnt/jornada/dos/netbsd

While you've copied most of the files you need, your device nodes must still be created. Go to the /dev directory on your flash disk, and run the MAKEDEV script installed by postinstall.

# cd /mnt/jornada/dev
# sh ./MAKEDEV all
#

You should then see a full array of NetBSD device nodes in /mnt/jornada/dev.

Finally, some programs expect to find the file /netbsd on the root filesystem. You could copy the kernel from the /dos partition onto the root partition, but that would waste space. Instead, just symlink it.

# cd /mnt/jornada
# ln -s dos/netbsd 
#

Further Configuration

Technically speaking, your system is now ready to go. Before you start, however, let's do some basic setup in the new system's /etc directory. It's much easier to do it at this point than to try to use the Jornada's elfin keyboard in single-user mode. One vital file is /etc/fstab.

/dev/wd0a /    ffs   rw,noatime,nodevmtime 1 1
/dev/wd0e /dos msdos -l,rw                 1 1

We can also do some basic setup in /etc/rc.conf. As we don't have swap space on the palmtop, disable swap at boot. We also don't need cron(8) on this system, as we'll be starting and stopping it on a whim. Finally, I expect to use NFS to expand the system's functionality, so we'll enable it now. In this case, I want sshd(8) running as well — I can do the major configuration of the device on a full-size keyboard from another system.

rc_configured=YES
hostname="palmtop.BlackHelicopters.org"
no_swap=YES
cron=NO
nfs_client=YES
sshd=YES

You can also configure anything else you like at this point, such as network interfaces.

Unmount and remove the flash card from your laptop, put it in the palmtop, and browse to the Storage Card. Double-click the hpcboot icon, and you'll drop into the boot loader.

On the "kernel" screen of hpcboot.exe, make sure that the path to the kernel is correct (probably \Storage Card\ in the first space, and netbsd in the second). From the drop-down menu, choose the machine type. On my 728, I've had the best results with the "HP Jornada 720 (Japanese)" option. Choose wd as the root file system. You might take a look at the "options" and "console" tabs. Be sure that "serial console" is not chosen, unless you have a serial console already set up. Once you're satisfied with the settings, hit the "boot" button. The screen will go dark, and you'll soon see the standard boot messages flowing past until a login prompt appears. Congratulations!

The Jornada 728 has an annoying problem when it's rebooted from NetBSD into WinCE; the screen becomes illegibly bright, and the contrast controls don't help. The only fix I've found for this is to remove the power supply, the backup battery, and the main battery, leave the palmtop alone for five minutes, and then restart the system.

Conclusion

You now have a basic, minimal UNIX system in the palm of your hand. Log in, create your account, and customize your environment as you please. Next time, we'll look at making our palmtop far more usable.

(The author wishes to acknowledge with gratitude the assistance of Alistair Crooks, Miles Nordin, and Valeriy E. Ushakov in preparing this article.)

Michael W. Lucas


Return to the BSD DevCenter

Copyright © 2009 O'Reilly Media, Inc.