Check each partition for swapspace before other filesystems.
authorTony Breeds <tony@bakeyournoodle.com>
Fri, 23 Oct 2009 03:05:50 +0000 (14:05 +1100)
committerTony Breeds <tony@bakeyournoodle.com>
Fri, 23 Oct 2009 03:40:03 +0000 (14:40 +1100)
Swapspace isn't guaranteed to be on a partition tagged as such, add a
little defensive programming in that case.

Signed-off-by: Tony Breeds <tony@bakeyournoodle.com>
Makefile
include/errors.h
second/fs.c
second/fs_swap.c [new file with mode: 0644]

index bcc221049eebf52568db26a8265455661d697e45..4c35c6758111650976534d149e67210da48fa205 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -83,7 +83,8 @@ HOSTCFLAGS = -O2 $(CFLAGS) -Wall -I/usr/include
 
 OBJS = second/crt0.o second/yaboot.o second/cache.o second/prom.o second/file.o \
        second/partition.o second/fs.o second/cfg.o second/setjmp.o second/cmdline.o \
 
 OBJS = second/crt0.o second/yaboot.o second/cache.o second/prom.o second/file.o \
        second/partition.o second/fs.o second/cfg.o second/setjmp.o second/cmdline.o \
-       second/fs_of.o second/fs_ext2.o second/fs_iso.o second/iso_util.o \
+       second/fs_of.o second/fs_ext2.o second/fs_iso.o second/fs_swap.o \
+       second/iso_util.o \
        lib/nosys.o lib/string.o lib/strtol.o lib/vsprintf.o lib/ctype.o lib/malloc.o lib/strstr.o
 
 ifeq ($(USE_MD5_PASSWORDS),y)
        lib/nosys.o lib/string.o lib/strtol.o lib/vsprintf.o lib/ctype.o lib/malloc.o lib/strstr.o
 
 ifeq ($(USE_MD5_PASSWORDS),y)
index e994dd571d55dbbc4fe9e8addefa7510aed779da..c0a67ed6d895e44a7220dca901c9b7a747c7173a 100644 (file)
@@ -34,6 +34,7 @@
 #define FILE_ERR_LENGTH         -10
 #define FILE_ERR_FSBUSY         -11
 #define FILE_ERR_BADDEV         -12
 #define FILE_ERR_LENGTH         -10
 #define FILE_ERR_FSBUSY         -11
 #define FILE_ERR_BADDEV         -12
+#define FILE_ERR_NOMEM          -13
 
 /* Device kind */
 #define FILE_DEVICE_BLOCK       1
 
 /* Device kind */
 #define FILE_DEVICE_BLOCK       1
index b748bed3e2d8401fc195ebae9f3fe80809609bd9..e7aeed0c05cccf4602025587e3742ff6167b8b5d 100644 (file)
@@ -27,6 +27,7 @@
 extern const struct fs_t       of_filesystem;
 extern const struct fs_t       of_net_filesystem;
 extern const struct fs_t       ext2_filesystem;
 extern const struct fs_t       of_filesystem;
 extern const struct fs_t       of_net_filesystem;
 extern const struct fs_t       ext2_filesystem;
+extern const struct fs_t        swap_filesystem;
 //extern const struct fs_t     iso_filesystem;
 
 /* Configurable filesystems */
 //extern const struct fs_t     iso_filesystem;
 
 /* Configurable filesystems */
@@ -41,6 +42,7 @@ extern const struct fs_t        reiserfs_filesystem;
 
 /* Filesystem handlers yaboot knows about */
 static const struct fs_t *block_filesystems[] = {
 
 /* Filesystem handlers yaboot knows about */
 static const struct fs_t *block_filesystems[] = {
+     &swap_filesystem,         /* swap signature checker */
      &ext2_filesystem,         /* ext2 */
 #ifdef CONFIG_FS_XFS
      &xfs_filesystem,                /* XFS */
      &ext2_filesystem,         /* ext2 */
 #ifdef CONFIG_FS_XFS
      &xfs_filesystem,                /* XFS */
diff --git a/second/fs_swap.c b/second/fs_swap.c
new file mode 100644 (file)
index 0000000..cfa5709
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ *  fs_swap.c - A filesystem driver to detect swapspace on a partition.
+ *
+ *  Copyright 2009 Tony Breeds, IBM Corporation
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ */
+
+#include "ctype.h"
+#include "types.h"
+#include "stddef.h"
+#include "stdlib.h"
+#include "file.h"
+#include "prom.h"
+#include "string.h"
+#include "partition.h"
+#include "fs.h"
+#include "errors.h"
+#include "debug.h"
+#include "bootinfo.h"
+
+#define ARRAY_SIZE(x)  (sizeof(x) / sizeof(x[0]))
+#define BLKCOUNT       2
+
+static struct {
+     char *magic;
+     int len, offset;
+} signatures[] = {
+     { "SWAP-SPACE", 10,  0xff6},  /*  4k Pages */
+     { "SWAPSPACE2", 10,  0xff6},
+     { "SWAP-SPACE", 10, 0xfff6},  /* 64k Pages */
+     { "SWAPSPACE2", 10, 0xfff6},
+};
+
+/* swap_open: Open a block device and look for swapspace magic.
+ *            If we find a valid signature the retuning FILE_ERR_NOTFOUND means
+ *            that no other filsystem drivers will check this partition */
+static int
+swap_open(struct boot_file_t* file, struct partition_t* part,
+          struct boot_fspec_t* fspec)
+{
+     int i;
+     unsigned char *buffer;
+     /* Make static to move into the BSS rather then the stack */
+     static unsigned char device_name[1024];
+
+     DEBUG_ENTER;
+     DEBUG_OPEN;
+
+     if (file->device_kind != FILE_DEVICE_BLOCK || part == NULL) {
+         DEBUG_LEAVE(FILE_ERR_BADDEV);
+         return FILE_ERR_BADDEV;
+     }
+
+     /* We assume that device is "short" and is correctly NULL terminsated */
+     strncpy(device_name, fspec->dev, 1020);
+     if (_machine != _MACH_bplan)
+         strcat(device_name, ":0");
+
+     DEBUG_F("Opening device: %s\n", device_name);
+     file->of_device = prom_open(device_name);
+     if (file->of_device == NULL) {
+          DEBUG_LEAVE(FILE_IOERR);
+          return FILE_IOERR;
+     }
+     DEBUG_F("file->of_device = %p\n", file->of_device);
+
+     /* If the signature is right on a block boundry we may need two blocks,
+      * so lets just allocate room for them now */
+     buffer = malloc(sizeof(unsigned char) * part->blocksize * BLKCOUNT);
+     if (buffer == NULL) {
+          DEBUG_LEAVE("malloc for disk buffer failed\n");
+          return FILE_ERR_NOMEM;
+     }
+
+     for(i=0; i< ARRAY_SIZE(signatures); i++) {
+         int blk = part->part_start + (signatures[i].offset / part->blocksize);
+          int rc = prom_readblocks(file->of_device, blk, BLKCOUNT, buffer);
+
+          /* FIXME: going past partition length */
+          DEBUG_F("Looking for %s @ offset 0x%x, rc == %d, blk=0x%x\n",
+                  signatures[i].magic, signatures[i].offset, rc, blk);
+
+          if (memcmp(&buffer[signatures[i].offset % part->blocksize],
+                     signatures[i].magic, signatures[i].len) == 0) {
+               free(buffer);
+               DEBUG_F("Found a swap signature\n");
+               DEBUG_LEAVE(FILE_ERR_NOTFOUND);
+               return FILE_ERR_NOTFOUND;
+          }
+     }
+
+     free(buffer);
+     prom_close(file->of_device);
+     file->of_device = NULL;
+
+     DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
+     return FILE_ERR_BAD_FSYS;
+}
+
+struct fs_t swap_filesystem =
+{
+     "swap signature checker",
+     swap_open,
+     NULL,
+     NULL,
+     NULL,
+};
+
+/*
+ * Local variables:
+ * c-file-style: "k&r"
+ * c-basic-offset: 5
+ * End:
+ */