]> git.ozlabs.org Git - yaboot.git/blob - second/fs_swap.c
fs_of: Increase the LOAD_BUFFER_SIZE to 32MB
[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 (part == NULL) {
63           DEBUG_F("No partions on %s, not checking for swap\n", device_name);
64           DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
65           return FILE_ERR_BAD_FSYS;
66      }
67
68      /* We assume that device is "short" and is correctly NULL terminsated */
69      strncpy(device_name, fspec->dev, 1020);
70      if (_machine != _MACH_bplan)
71           strcat(device_name, ":0");
72
73      DEBUG_F("Opening device: %s\n", device_name);
74      file->of_device = prom_open(device_name);
75      if (file->of_device == NULL) {
76           DEBUG_LEAVE(FILE_IOERR);
77           return FILE_IOERR;
78      }
79      DEBUG_F("file->of_device = %p\n", file->of_device);
80
81      /* If the signature is right on a block boundry we may need two blocks,
82       * so lets just allocate room for them now */
83      buffer = malloc(sizeof(unsigned char) * part->blocksize * BLKCOUNT);
84      if (buffer == NULL) {
85           DEBUG_LEAVE("malloc for disk buffer failed\n");
86           return FILE_ERR_NOMEM;
87      }
88
89      for(i=0; i< ARRAY_SIZE(signatures); i++) {
90           int blk = part->part_start + (signatures[i].offset / part->blocksize);
91           int rc = prom_readblocks(file->of_device, blk, BLKCOUNT, buffer);
92
93           /* FIXME: going past partition length */
94           DEBUG_F("Looking for %s @ offset 0x%x, rc == %d, blk=0x%x\n",
95                   signatures[i].magic, signatures[i].offset, rc, blk);
96
97           if (memcmp(&buffer[signatures[i].offset % part->blocksize],
98                      signatures[i].magic, signatures[i].len) == 0) {
99                free(buffer);
100                DEBUG_F("Found a swap signature\n");
101                DEBUG_LEAVE(FILE_ERR_NOTFOUND);
102                return FILE_ERR_NOTFOUND;
103           }
104      }
105
106      free(buffer);
107      prom_close(file->of_device);
108      file->of_device = NULL;
109
110      DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
111      return FILE_ERR_BAD_FSYS;
112 }
113
114 struct fs_t swap_filesystem =
115 {
116      "swap signature checker",
117      swap_open,
118      NULL,
119      NULL,
120      NULL,
121 };
122
123 /*
124  * Local variables:
125  * c-file-style: "k&r"
126  * c-basic-offset: 5
127  * End:
128  */