2 * fs_swap.c - A filesystem driver to detect swapspace on a partition.
4 * Copyright 2009 Tony Breeds, IBM Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include "partition.h"
34 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
41 { "SWAP-SPACE", 10, 0xff6}, /* 4k Pages */
42 { "SWAPSPACE2", 10, 0xff6},
43 { "SWAP-SPACE", 10, 0xfff6}, /* 64k Pages */
44 { "SWAPSPACE2", 10, 0xfff6},
47 /* swap_open: Open a block device and look for swapspace magic.
48 * If we find a valid signature the retuning FILE_ERR_NOTFOUND means
49 * that no other filsystem drivers will check this partition */
51 swap_open(struct boot_file_t* file, struct partition_t* part,
52 struct boot_fspec_t* fspec)
55 unsigned char *buffer;
56 /* Make static to move into the BSS rather then the stack */
57 static char device_name[1024];
62 if (file->device_kind != FILE_DEVICE_BLOCK || part == NULL) {
63 DEBUG_LEAVE(FILE_ERR_BADDEV);
64 return FILE_ERR_BADDEV;
67 /* We assume that device is "short" and is correctly NULL terminsated */
68 strncpy(device_name, fspec->dev, 1020);
69 if (_machine != _MACH_bplan)
70 strcat(device_name, ":0");
72 DEBUG_F("Opening device: %s\n", device_name);
73 file->of_device = prom_open(device_name);
74 if (file->of_device == NULL) {
75 DEBUG_LEAVE(FILE_IOERR);
78 DEBUG_F("file->of_device = %p\n", file->of_device);
80 /* If the signature is right on a block boundry we may need two blocks,
81 * so lets just allocate room for them now */
82 buffer = malloc(sizeof(unsigned char) * part->blocksize * BLKCOUNT);
84 DEBUG_LEAVE("malloc for disk buffer failed\n");
85 return FILE_ERR_NOMEM;
88 for(i=0; i< ARRAY_SIZE(signatures); i++) {
89 int blk = part->part_start + (signatures[i].offset / part->blocksize);
90 int rc = prom_readblocks(file->of_device, blk, BLKCOUNT, buffer);
92 /* FIXME: going past partition length */
93 DEBUG_F("Looking for %s @ offset 0x%x, rc == %d, blk=0x%x\n",
94 signatures[i].magic, signatures[i].offset, rc, blk);
96 if (memcmp(&buffer[signatures[i].offset % part->blocksize],
97 signatures[i].magic, signatures[i].len) == 0) {
99 DEBUG_F("Found a swap signature\n");
100 DEBUG_LEAVE(FILE_ERR_NOTFOUND);
101 return FILE_ERR_NOTFOUND;
106 prom_close(file->of_device);
107 file->of_device = NULL;
109 DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
110 return FILE_ERR_BAD_FSYS;
113 struct fs_t swap_filesystem =
115 "swap signature checker",
124 * c-file-style: "k&r"