freebsd-nq/sys/i386/boot/cdboot/malloc.c
Joerg Wunsch c38b4f3b91 (Part #2, after the Internet link broke totally yesterday.)
This is the long-threatened ISO 9660 CD-ROM bootstrap code.

This work has been sponsored by Plutotech International, Inc (who paid
the initial work), and interface business GmbH (where i did most of
the work).  A big thanks also goes to Bruce Evans, for his continuing
help and answering my stupid questions.

The code is basically functioning, with the following caveats:

. Rock Ridge attributes are not yet supported.
. Only SCSI CD-ROMs are supported, since i fail to see any possibility
  to determine the drive type using BIOS functions.  (Even for hard disks,
  this determination is done by a big hack only.)
. El Torito specifies a lot of crap and useless misfeatures, but crucial
  things like the ability to figure out the CD TOC have been ``forgotten''.
  Thus, if you wanna boot a multisession CD, you need to know at which CD
  block your session starts, and need to speciffy it using the @ clause.

. None of the CD-ROM controllers i've seen so far implements the full
  El Torito specification at all.  Adaptec is probably the closest, but
  they miss on non-emulation booting (which would be the most logical
  choice for us).  Thus, the current code bloats the 7.5 KB boot code
  up to 1.44 MB, in order to fake a `floppy' image.

  If you wanna use it, specify this file as the boot image on the
  command-line of the mksiosfs command (option -b).

  Caveat emptor: some versions of the Adaptec BIOS might even fail to
  access the CD-ROM at all, using the BIOS functions.  I think i've
  notice this for ver 1.26, the code has been tested with ver 1.23.

The boot string is as follows:

        [@sess-start] [filename] [-flags]

sess-start      Extend # where the last session starts, measured in
                CD-ROM blocks.

filename        As usual, but the input is case-insensitive by now
                (since we  don't grok RR anyway).

flags           As usual, but -C (use CDROM root f/s) is default, so
                specifying -C will decactivate this option (which is
                probably not what you want :).

A lot of cleanup work is probably required, and some of the files
could/should be merged back to biosboot, perhaps made conditional on
some #ifdef.  The malloc implementation that comes with cdboot might
also be useful for kzipboot.  (I needed a malloc() since the root dir
ain't fixed in size on a CD.)

I've been testing all this with a 2.2-STABLE as the base for biosboot.
I don't expect too many surprises, although i know the biosboot stuff
has been changed a lot in -current lately.  I'm sure Bruce will
comment on all this here anyway. :-)
1997-07-11 05:52:41 +00:00

180 lines
4.1 KiB
C

/*
* Copyright © 1997 Pluto Technologies International, Inc. Boulder CO
* Copyright © 1997 interface business GmbH, Dresden.
* All rights reserved.
*
* This code was written by Jörg Wunsch, Dresden.
* Direct comments to <joerg_wunsch@interface-business.de>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*/
/*
* Simple memory allocator for the bootstrap loader. Probably suffers
* a lot from fragmentation.
*/
#include "boot.h"
#include <stddef.h>
/* ``Nobody will ever need more than 640 KB of RAM.'' :-) */
#define MAXBRK (640 * 1024 * 1024)
/* allocation unit */
#define NCHUNKS 2048
struct chunk
{
struct chunk *next;
size_t len;
};
static void *brkval;
static struct chunk *freelist;
void *
malloc(size_t len)
{
struct chunk *p, *q, *oldp;
size_t nelems;
nelems = (len + sizeof(struct chunk) - 1) / sizeof(struct chunk) + 1;
/*
* First, see if we can satisfy the request from the freelist.
*/
for (p = freelist, oldp = 0;
p && p != (struct chunk *)brkval;
oldp = p, p = p->next) {
if (p->len > nelems) {
/* chunk is larger, shorten, and return the tail */
p->len -= nelems;
q = p + p->len;
q->next = 0;
q->len = nelems;
q++;
return (void *)q;
}
if (p->len == nelems) {
/* exact match, remove from freelist */
if (oldp == 0)
freelist = p->next;
else
oldp->next = p->next;
p->next = 0;
p++;
return (void *)p;
}
}
/*
* Nothing found on freelist, try obtaining more space.
*/
if (brkval == 0)
brkval = &end;
q = p = (struct chunk *)brkval;
if ((int)(p + NCHUNKS) > MAXBRK)
return 0;
p += NCHUNKS;
brkval = p;
q->next = p;
q->len = NCHUNKS;
if (oldp == 0)
freelist = q;
else {
if (oldp + oldp->len == q) {
/* extend last chunk */
oldp->len += NCHUNKS;
oldp->next = p;
} else
oldp->next = q;
}
return malloc(len);
}
void
free(void *ptr)
{
struct chunk *p, *q, *oldp;
if (ptr == 0)
return;
q = (struct chunk *)ptr;
q--;
if (q->next != 0) {
printf("malloc error: botched ptr to free()\n");
return;
}
/*
* Walk the freelist, and insert in the correct sequence.
*/
for (p = freelist, oldp = 0;
p && p != (struct chunk *)brkval;
oldp = p, p = p->next) {
if ((unsigned)p > (unsigned)q) {
if (q + q->len == p) {
/* aggregate with next chunk */
q->len += p->len;
q->next = p->next;
p = p->next;
}
if (oldp) {
if (oldp + oldp->len == q) {
/* aggregate with previous chunk */
oldp->len += q->len;
oldp->next = p;
} else {
/* insert into chain */
q->next = p;
oldp->next = q;
}
return;
}
q->next = p;
freelist = q;
}
}
if (oldp) {
/* we are topmost */
if (oldp + oldp->len == q) {
/* aggregate with previous chunk */
oldp->len += q->len;
oldp->next = p;
} else {
oldp->next = q;
q->next = p;
}
return;
}
/* we are alone on the freelist */
freelist = q;
}