skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home Products Teensy Blog Forum
You are here: MP3 Player Technical Docs Old MP3 Player Design HD Write Script

PJRC Store
Main Board, $150
LCD & Pushbuttons, $42
LCD/Backlight/PB, $77
IDE Cable, $9
Complete Parts List
MP3 Player
Main Page
Detailed Info
User Photo Gallery
Connecting The Board
Firmware Download
Side Projects
Technical Docs
Freq. Asked Questions
FAQ #2
News And Updates


icon: new design
Click For New Design
This new design has replaced the old one. The design shown on this page is obsolete. The new design has more features, is flash upgradable, is much easier to use, costs less, and is actually available if you'd like to buy one! It's better than this old design is almost every way. These old pages are still available, mostly for reference.

Why Do I Need To Use This Script

The firmware expects the MP3 files to be written to the drive in a very simple way, which is not compatible with ordinary filesystems used by Windows and Linux. If you were to just copy the files to the hard drive, using a program like the windows explorer, they would be written using either FAT16 or FAT32 the filesystem, depending on how the drive was formatted. Unfortunately, the firmware for this project is not smart enough to read ordinary filesystems, so you must write the file to the drive using a particular format, which this script will produce.

The format is pretty simple. The first 256 sectors are reserved for a table that tells where each MP3 file begins on the drive. The firmware only reads the first 8 of these sectors, but it could be modified to read more of them, if needed. The MP3 files are written, in order, beginning at the 257th sector. Zeros are appended to each file so that it fills an integral number of sectors. All the files must be written in a continuous group of sectors, and unused sectors are not allowed between files. This simple format is not very flexible, since it basically means that it is impossible to remove a file without rearranging all of the sectors that follow it. It also means that you must completely dedicate a drive to the player. Because the data begins at sector 0, which is ordinarily the master boot record with the drive's partition table, the drives partitioning is ignored and gets completely erased by writing the MP3 files.

This script just copies all the files from a directory onto the drive, writing the 256 sector table and then all of the data. Though it is possible to add additional files after originally writing, this script doesn't provide a way to do it.

Perl Script that Writes to the Hard Drive

You may download this code in a ZIP file.

This code is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.


#! /usr/bin/perl

# make a "raw" hard drive image for use with my
# initial attempts at the mp3 player.

# see http://www.pjrc.com/tech/mp3/ for more info.

# WARNING: be very careful using this program.  All it
# does is create a massive stream of data, but by using
# the shell's redirect to /dev/hdc (or whatever device
# represents your raw drive drive), you will completely
# overwrite all data on the hard drive.  If you type
# the wrong device filename, you could easily destroy
# all of the data on your computer!!  Be careful.

# Of course, this script is useless without an operating
# system that provides a raw device that can accept the
# data.... Linux works very nicely.  If all you have is
# crap from Microsoft, maybe this is a good time to
# consider upgrading!

#  This code is an original work by Paul Stoffregen, written
#  in December 1999.  This code has been placed in the
#  public domain.  You may use it without any restrictions.
#  You may include it in your own projects, even commercial
#  (for profit) products.

#  This code is distributed in the hope that they will be useful,
#  but without any warranty; without even the implied warranty of
#  merchantability or fitness for a particular purpose.



$usage = "Typical Usage: mk_hdimg <directory> > /dev/hdc\n";

$dir = $ARGV[0];
$#ARGV >= 0 || die $usage;
-d $dir || die $usage;
 
opendir(DIR, $dir) || die "Unable to read $dir\n";
@dirfile = readdir(DIR);
closedir(DIR);

@dirfile = sort @dirfile;

$n = 0;
$blk = 256;
for ($i=0; $i < $#dirfile; $i++) {
	$f = "$dir/$dirfile[$i]";
	next unless -f $f && -r $f;
	$s = -s $f;
	$block[$n] = $blk;
	$file[$n] = $f;
	$blk += int(($s + 511) / 512);
	$n++;
}

$n = 32767 if $n > 32767;
warn "$n files to copy\n";

for ($i=0; $i < $n; $i++) {
	print pack("I", $block[$i]);
}
for ( ; $i < 32768; $i++) {
	print pack("I", 0);
}

for ($i=0; $i < $n; $i++) {
	$f = $file[$i];
	$s = -s $f;
	$r = int(($s + 511) / 512) * 512 - $s;
	warn "Copy file $f, $s bytes, $r pad bytes\n";
	open(FILE, $f);
	$ss = read(FILE, $mp3, $s);
	$ss == $s || die "Error, $ss bytes read, should be $s\n";
	print $mp3;
	close(FILE);
	print pack("x$r");
}


Paul's Homebrew MP3 Player, Paul Stoffregen.
Designed and constructed Winter, 2000.
http://www.pjrc.com/tech/mp3/hd_script.html
Last updated: February 23, 2005
Status: More info to come... just a couple photos for now.
Questions, Comments?? <paul@pjrc.com>