]> git.ozlabs.org Git - yaboot.git/blob - second/fs_swap.c
87da8775f85c8db55e7101261e88a4ac56f713e6
[yaboot.git] / second / fs_swap.c
1 /*
2  *  fs_swap.c - A filesystem driver to detect swapspace on a partition.
3  *
4  *  Copyright 2009 Tony Breeds, IBM Corporation
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "ctype.h"
22 #include "types.h"
23 #include "stddef.h"
24 #include "stdlib.h"
25 #include "file.h"
26 #include "prom.h"
27 #include "string.h"
28 #include "partition.h"
29 #include "fs.h"
30 #include "errors.h"
31 #include "debug.h"
32 #include "bootinfo.h"
33
34 #define ARRAY_SIZE(x)   (sizeof(x) / sizeof(x[0]))
35 #define BLKCOUNT        2
36
37 static struct {
38      char *magic;
39      int len, offset;
40 } signatures[] = {
41      { "SWAP-SPACE", 10,  0xff6},  /*  4k Pages */
42      { "SWAPSPACE2", 10,  0xff6},
43      { "SWAP-SPACE", 10, 0xfff6},  /* 64k Pages */
44      { "SWAPSPACE2", 10, 0xfff6},
45 };
46
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 */
50 static int
51 swap_open(struct boot_file_t* file, struct partition_t* part,
52           struct boot_fspec_t* fspec)
53 {
54      int i;
55      unsigned char *buffer;
56      /* Make static to move into the BSS rather then the stack */
57      static char device_name[1024];
58
59      DEBUG_ENTER;
60      DEBUG_OPEN;
61
62      if (file->device_kind != FILE_DEVICE_BLOCK || part == NULL) {
63           DEBUG_LEAVE(FILE_ERR_BADDEV);
64           return FILE_ERR_BADDEV;
65      }
66
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");
71
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);
76           return FILE_IOERR;
77      }
78      DEBUG_F("file->of_device = %p\n", file->of_device);
79
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);
83      if (buffer == NULL) {
84           DEBUG_LEAVE("malloc for disk buffer failed\n");
85           return FILE_ERR_NOMEM;
86      }
87
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);
91
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);
95
96           if (memcmp(&buffer[signatures[i].offset % part->blocksize],
97                      signatures[i].magic, signatures[i].len) == 0) {
98                free(buffer);
99                DEBUG_F("Found a swap signature\n");
100                DEBUG_LEAVE(FILE_ERR_NOTFOUND);
101                return FILE_ERR_NOTFOUND;
102           }
103      }
104
105      free(buffer);
106      prom_close(file->of_device);
107      file->of_device = NULL;
108
109      DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
110      return FILE_ERR_BAD_FSYS;
111 }
112
113 struct fs_t swap_filesystem =
114 {
115      "swap signature checker",
116      swap_open,
117      NULL,
118      NULL,
119      NULL,
120 };
121
122 /*
123  * Local variables:
124  * c-file-style: "k&r"
125  * c-basic-offset: 5
126  * End:
127  */