- char bootbits[1024]; /* Space for disklabel etc. */
- unsigned int version;
- unsigned int last_page;
- unsigned int nr_badpages;
- unsigned int padding[125];
- unsigned int badpages[1];
-} *p;
-
-static void init_signature_page()
-{
- pagesize = getpagesize();
-
-#ifdef PAGE_SIZE
- if (pagesize != PAGE_SIZE)
- errorMsg("Assuming pages of size %d\n", pagesize);
-#endif
- signature_page = (int *) xmalloc(pagesize);
- memset(signature_page, 0, pagesize);
- p = (struct swap_header_v1 *) signature_page;
-}
-
-static void write_signature(char *sig)
-{
- char *sp = (char *) signature_page;
-
- strncpy(sp + pagesize - 10, sig, 10);
-}
-
-#define V0_MAX_PAGES (8 * (pagesize - 10))
-/* Before 2.2.0pre9 */
-#define V1_OLD_MAX_PAGES ((0x7fffffff / pagesize) - 1)
-/* Since 2.2.0pre9:
- error if nr of pages >= SWP_OFFSET(SWP_ENTRY(0,~0UL))
- with variations on
- #define SWP_ENTRY(type,offset) (((type) << 1) | ((offset) << 8))
- #define SWP_OFFSET(entry) ((entry) >> 8)
- on the various architectures. Below the result - yuk.
-
- Machine pagesize SWP_ENTRY SWP_OFFSET bound+1 oldbound+2
- i386 2^12 o<<8 e>>8 1<<24 1<<19
- mips 2^12 o<<15 e>>15 1<<17 1<<19
- alpha 2^13 o<<40 e>>40 1<<24 1<<18
- m68k 2^12 o<<12 e>>12 1<<20 1<<19
- sparc 2^{12,13} (o&0x3ffff)<<9 (e>>9)&0x3ffff 1<<18 1<<{19,18}
- sparc64 2^13 o<<13 e>>13 1<<51 1<<18
- ppc 2^12 o<<8 e>>8 1<<24 1<<19
- armo 2^{13,14,15} o<<8 e>>8 1<<24 1<<{18,17,16}
- armv 2^12 o<<9 e>>9 1<<23 1<<19
-
- assuming that longs have 64 bits on alpha and sparc64 and 32 bits elsewhere.
-
- The bad part is that we need to know this since the kernel will
- refuse a swap space if it is too large.
-*/
-/* patch from jj - why does this differ from the above? */
-#if defined(__alpha__)
-#define V1_MAX_PAGES ((1 << 24) - 1)
-#elif defined(__mips__)
-#define V1_MAX_PAGES ((1 << 17) - 1)
-#elif defined(__sparc_v9__)
-#define V1_MAX_PAGES ((3 << 29) - 1)
-#elif defined(__sparc__)
-#define V1_MAX_PAGES (pagesize == 8192 ? ((3 << 29) - 1) : ((1 << 18) - 1))
-#else
-#define V1_MAX_PAGES V1_OLD_MAX_PAGES
-#endif
-/* man page now says:
-The maximum useful size of a swap area now depends on the architecture.
-It is roughly 2GB on i386, PPC, m68k, ARM, 1GB on sparc, 512MB on mips,
-128GB on alpha and 3TB on sparc64.
-*/
-
-#define MAX_BADPAGES ((pagesize-1024-128*sizeof(int)-10)/sizeof(int))
-
-static void bit_set(unsigned int *addr, unsigned int nr)
-{
- unsigned int r, m;
-
- addr += nr / (8 * sizeof(int));
-
- r = *addr;
- m = 1 << (nr & (8 * sizeof(int) - 1));
-
- *addr = r | m;
-}
-
-static int bit_test_and_clear(unsigned int *addr, unsigned int nr)
-{
- unsigned int r, m;
-
- addr += nr / (8 * sizeof(int));
-
- r = *addr;
- m = 1 << (nr & (8 * sizeof(int) - 1));
-
- *addr = r & ~m;
- return (r & m) != 0;
-}
-
-
-void die(const char *str)
-{
- errorMsg("%s\n", str);
- exit(FALSE);
-}
-
-void page_ok(int page)
-{
- if (version == 0)
- bit_set(signature_page, page);
-}
-
-void page_bad(int page)
-{
- if (version == 0)
- bit_test_and_clear(signature_page, page);
- else {
- if (badpages == MAX_BADPAGES)
- die("too many bad pages");
- p->badpages[badpages] = page;
- }
- badpages++;
-}
-
-void check_blocks(void)
-{
- unsigned int current_page;
- int do_seek = 1;
- char *buffer;
-
- buffer = xmalloc(pagesize);
- current_page = 0;
- while (current_page < PAGES) {
- if (!check) {
- page_ok(current_page++);
- continue;
- }
- if (do_seek && lseek(DEV, current_page * pagesize, SEEK_SET) !=
- current_page * pagesize)
- die("seek failed in check_blocks");
- if ((do_seek = (pagesize != read(DEV, buffer, pagesize)))) {
- page_bad(current_page++);
- continue;
- }
- page_ok(current_page++);
- }
- if (badpages == 1)
- printf("one bad page\n");
- else if (badpages > 1)
- printf("%d bad pages\n", badpages);
-}
-
-static long valid_offset(int fd, int offset)
-{
- char ch;
-
- if (lseek(fd, offset, 0) < 0)
- return 0;
- if (read(fd, &ch, 1) < 1)
- return 0;
- return 1;
-}
-
-static int find_size(int fd)
-{
- unsigned int high, low;
-
- low = 0;
- for (high = 1; high > 0 && valid_offset(fd, high); high *= 2)
- low = high;
- while (low < high - 1) {
- const int mid = (low + high) / 2;
-
- if (valid_offset(fd, mid))
- low = mid;
- else
- high = mid;
- }
- return (low + 1);
-}
-
-/* return size in pages, to avoid integer overflow */
-static long get_size(const char *file)
+/* char bootbits[1024]; Space for disklabel etc. */
+ uint32_t version; /* second kbyte, word 0 */
+ uint32_t last_page; /* 1 */
+ uint32_t nr_badpages; /* 2 */
+ char sws_uuid[16]; /* 3,4,5,6 */
+ char sws_volume[16]; /* 7,8,9,10 */
+ uint32_t padding[117]; /* 11..127 */
+ uint32_t badpages[1]; /* 128 */
+ /* total 129 32-bit words in 2nd kilobyte */
+} FIX_ALIASING;
+
+#define NWORDS 129
+#define hdr ((struct swap_header_v1*)bb_common_bufsiz1)
+
+struct BUG_sizes {
+ char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1];
+ char bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1];
+};
+
+/* Stored without terminating NUL */
+static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
+
+int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int mkswap_main(int argc UNUSED_PARAM, char **argv)