-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <sys/ioctl.h> /* for _IO */
-#include <sys/utsname.h>
-#include <asm/page.h> /* for PAGE_SIZE and PAGE_SHIFT */
- /* we also get PAGE_SIZE via getpagesize() */
-#include "busybox.h"
-
-#ifndef _IO
-/* pre-1.3.45 */
-static const int BLKGETSIZE = 0x1260;
-#else
-/* same on i386, m68k, arm; different on alpha, mips, sparc, ppc */
-#define BLKGETSIZE _IO(0x12,96)
-#endif
-
-static char *device_name = NULL;
-static int DEV = -1;
-static long PAGES = 0;
-static int check = 0;
-static int badpages = 0;
-static int version = -1;
-
-#define MAKE_VERSION(p,q,r) (65536*(p) + 256*(q) + (r))
-
-/*
- * The definition of the union swap_header uses the constant PAGE_SIZE.
- * Unfortunately, on some architectures this depends on the hardware model,
- * and can only be found at run time -- we use getpagesize().
- */
-
-static int pagesize;
-static unsigned int *signature_page;
-
-static struct swap_header_v1 {
- 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 inline void init_signature_page(void)
-{
- pagesize = getpagesize();
-
-#ifdef PAGE_SIZE
- if (pagesize != PAGE_SIZE)
- bb_error_msg("Assuming pages of size %d", pagesize);
-#endif
- signature_page = (unsigned int *) xmalloc(pagesize);
- memset(signature_page, 0, pagesize);
- p = (struct swap_header_v1 *) signature_page;
-}
-
-static inline 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 inline 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;
-}
-
-
-static void page_ok(int page)
-{
- if (version == 0)
- bit_set(signature_page, page);
-}
-
-static inline void page_bad(int page)
-{
- if (version == 0)
- bit_test_and_clear(signature_page, page);
- else {
- if (badpages == MAX_BADPAGES)
- bb_error_msg_and_die("too many bad pages");
- p->badpages[badpages] = page;
- }
- badpages++;
-}