freebsd-skq/sys/boot/common/util.c
Pawel Jakub Dawidek a0e2fdedd1 - Split code shared by almost any boot loader into separate files and
clean up most layering violations:

	sys/boot/i386/common/rbx.h:

		RBX_* defines
		OPT_SET()
		OPT_CHECK()

	sys/boot/common/util.[ch]:

		memcpy()
		memset()
		memcmp()
		bcpy()
		bzero()
		bcmp()
		strcmp()
		strncmp() [new]
		strcpy()
		strcat()
		strchr()
		strlen()
		printf()

	sys/boot/i386/common/cons.[ch]:

		ioctrl
		putc()
		xputc()
		putchar()
		getc()
		xgetc()
		keyhit() [now takes number of seconds as an argument]
		getstr()

	sys/boot/i386/common/drv.[ch]:

		struct dsk
		drvread()
		drvwrite() [new]
		drvsize() [new]

	sys/boot/common/crc32.[ch] [new]

	sys/boot/common/gpt.[ch] [new]

- Teach gptboot and gptzfsboot about new files. I haven't touched the
  rest, but there is still a lot of code duplication to be removed.

- Implement full GPT support. Currently we just read primary header and
  partition table and don't care about checksums, etc. After this change we
  verify checksums of primary header and primary partition table and if
  there is a problem we fall back to backup header and backup partition
  table.

- Clean up most messages to use prefix of boot program, so in case of an
  error we know where the error comes from, eg.:

	gptboot: unable to read primary GPT header

- If we can't boot, print boot prompt only once and not every five
  seconds.

- Honour newly added GPT attributes:

	bootme - this is bootable partition
	bootonce - try to boot from this partition only once
	bootfailed - we failed to boot from this partition

- Change boot order of gptboot to the following:

	1. Try to boot from all the partitions that have both 'bootme'
	   and 'bootonce' attributes one by one.
	2. Try to boot from all the partitions that have only 'bootme'
	   attribute one by one.
	3. If there are no partitions with 'bootme' attribute, boot from
	   the first UFS partition.

- The 'bootonce' functionality is implemented in the following way:

	1. Walk through all the partitions and when 'bootonce'
	   attribute is found without 'bootme' attribute, remove
	   'bootonce' attribute and set 'bootfailed' attribute.
	   'bootonce' attribute alone means that we tried to boot from
	   this partition, but boot failed after leaving gptboot and
	   machine was restarted.
	2. Find partition with both 'bootme' and 'bootonce' attributes.
	3. Remove 'bootme' attribute.
	4. Try to execute /boot/loader or /boot/kernel/kernel from that
	   partition. If succeeded we stop here.
	5. If execution failed, remove 'bootonce' and set 'bootfailed'.
	6. Go to 2.

   If whole boot succeeded there is new /etc/rc.d/gptboot script coming
   that will log all partitions that we failed to boot from (the ones with
   'bootfailed' attribute) and will remove this attribute. It will also
   find partition with 'bootonce' attribute - this is the partition we
   booted from successfully. The script will log success and remove the
   attribute.

   All the GPT updates we do here goes to both primary and backup GPT if
   they are valid. We don't touch headers or partition tables when
   checksum doesn't match.

Reviewed by:	arch (Message-ID: <20100917234542.GE1902@garage.freebsd.pl>)
Obtained from:	Wheel Systems Sp. z o.o. http://www.wheelsystems.com
MFC after:	2 weeks
2010-09-24 19:49:12 +00:00

177 lines
2.8 KiB
C

/*-
* Copyright (c) 1998 Robert Nordier
* Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms are freely
* permitted provided that the above copyright notice and this
* paragraph and the following disclaimer are duplicated in all
* such forms.
*
* This software is provided "AS IS" and without any express or
* implied warranties, including, without limitation, the implied
* warranties of merchantability and fitness for a particular
* purpose.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <stdarg.h>
#include "cons.h"
#include "util.h"
void
memcpy(void *dst, const void *src, int len)
{
const char *s = src;
char *d = dst;
while (len--)
*d++ = *s++;
}
void
memset(void *b, int c, size_t len)
{
char *bp = b;
while (len--)
*bp++ = (unsigned char)c;
}
int
memcmp(const void *b1, const void *b2, size_t len)
{
const unsigned char *p1, *p2;
for (p1 = b1, p2 = b2; len > 0; len--, p1++, p2++) {
if (*p1 != *p2)
return ((*p1) - (*p2));
}
return (0);
}
int
strcmp(const char *s1, const char *s2)
{
for (; *s1 == *s2 && *s1 != '\0'; s1++, s2++)
;
return ((unsigned char)*s1 - (unsigned char)*s2);
}
int
strncmp(const char *s1, const char *s2, size_t len)
{
for (; *s1 == *s2 && *s1 != '\0' && len > 0; len--, s1++, s2++)
;
return ((unsigned char)*s1 - (unsigned char)*s2);
}
void
strcpy(char *dst, const char *src)
{
while (*src != '\0')
*dst++ = *src++;
*dst = '\0';
}
void
strcat(char *dst, const char *src)
{
while (*dst != '\0')
dst++;
while (*src != '\0')
*dst++ = *src++;
*dst = '\0';
}
char *
strchr(const char *s, char ch)
{
for (; *s != '\0'; s++) {
if (*s == ch)
return ((char *)(uintptr_t)(const void *)s);
}
return (NULL);
}
size_t
strlen(const char *s)
{
size_t len = 0;
while (*s++ != '\0')
len++;
return (len);
}
void
printf(const char *fmt, ...)
{
va_list ap;
const char *hex = "0123456789abcdef";
char buf[10], *s;
unsigned long long u;
int c, l;
va_start(ap, fmt);
while ((c = *fmt++) != '\0') {
if (c != '%') {
putchar(c);
continue;
}
l = 0;
nextfmt:
c = *fmt++;
switch (c) {
case 'l':
l++;
goto nextfmt;
case 'c':
putchar(va_arg(ap, int));
break;
case 's':
for (s = va_arg(ap, char *); *s != '\0'; s++)
putchar(*s);
break;
case 'd': /* A lie, always prints unsigned */
case 'u':
case 'x':
switch (l) {
case 2:
u = va_arg(ap, unsigned long long);
break;
case 1:
u = va_arg(ap, unsigned long);
break;
default:
u = va_arg(ap, unsigned int);
break;
}
s = buf;
if (c == 'd' || c == 'u') {
do
*s++ = '0' + (u % 10U);
while (u /= 10);
} else {
do
*s++ = hex[u & 0xfu];
while (u >>= 4);
}
while (--s >= buf)
putchar(*s);
break;
}
}
va_end(ap);
}