Merge "Fix supplicant start"
diff --git a/bugmailer/bugmailer.sh b/bugmailer/bugmailer.sh
index ba8eeb7..93d1c8a 100755
--- a/bugmailer/bugmailer.sh
+++ b/bugmailer/bugmailer.sh
@@ -1,5 +1,12 @@
 #!/system/bin/sh
 
+# Do not allow bugreports on user builds unless USB debugging
+# is enabled.
+if [ "x$(getprop ro.build.type)" = "xuser" -a \
+     "x$(getprop init.svc.adbd)" != "xrunning" ]; then
+  exit 0
+fi
+
 timestamp=`date +'%Y-%m-%d-%H-%M-%S'`
 storagePath="$EXTERNAL_STORAGE/bugreports"
 bugreport=$storagePath/bugreport-$timestamp
diff --git a/ext4_utils/backed_block.c b/ext4_utils/backed_block.c
index f8399ef..c0070f1 100644
--- a/ext4_utils/backed_block.c
+++ b/ext4_utils/backed_block.c
@@ -150,7 +150,7 @@
 /* Frees the memory used by the linked list of data blocks */
 void free_data_blocks()
 {
-        if (!data_blocks) return;
+	if (!data_blocks) return;
 	struct data_block *db = data_blocks;
 	while (db) {
 		struct data_block *next = db->next;
@@ -167,6 +167,6 @@
 		free(db);
 		db = next;
 	}
-        data_blocks = NULL;
-        last_used = NULL;
+	data_blocks = NULL;
+	last_used = NULL;
 }
diff --git a/ext4_utils/ext4_utils.c b/ext4_utils/ext4_utils.c
index d41b92d..3d5895e 100644
--- a/ext4_utils/ext4_utils.c
+++ b/ext4_utils/ext4_utils.c
@@ -460,6 +460,7 @@
 #elif defined(__APPLE__) && defined(__MACH__)
 	ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &size);
 #else
+	close(fd);
 	return 0;
 #endif
 
diff --git a/ext4_utils/make_ext4fs_main.c b/ext4_utils/make_ext4fs_main.c
index d616c6d..4e13ffa 100644
--- a/ext4_utils/make_ext4fs_main.c
+++ b/ext4_utils/make_ext4fs_main.c
@@ -30,115 +30,115 @@
 
 static void usage(char *path)
 {
-        fprintf(stderr, "%s [ -l <len> ] [ -j <journal size> ] [ -b <block_size> ]\n", basename(path));
-        fprintf(stderr, "    [ -g <blocks per group> ] [ -i <inodes> ] [ -I <inode size> ]\n");
-        fprintf(stderr, "    [ -L <label> ] [ -f ] [ -a <android mountpoint> ]\n");
-        fprintf(stderr, "    [ -z | -s ] [ -t ] [ -w ] [ -c ] [ -J ]\n");
-        fprintf(stderr, "    <filename> [<directory>]\n");
+	fprintf(stderr, "%s [ -l <len> ] [ -j <journal size> ] [ -b <block_size> ]\n", basename(path));
+	fprintf(stderr, "    [ -g <blocks per group> ] [ -i <inodes> ] [ -I <inode size> ]\n");
+	fprintf(stderr, "    [ -L <label> ] [ -f ] [ -a <android mountpoint> ]\n");
+	fprintf(stderr, "    [ -z | -s ] [ -t ] [ -w ] [ -c ] [ -J ]\n");
+	fprintf(stderr, "    <filename> [<directory>]\n");
 }
 
 int main(int argc, char **argv)
 {
-        int opt;
-        const char *filename = NULL;
-        const char *directory = NULL;
-        char *mountpoint = "";
-        int android = 0;
-        int gzip = 0;
-        int sparse = 0;
-        int crc = 0;
-        int wipe = 0;
-        int init_itabs = 0;
+	int opt;
+	const char *filename = NULL;
+	const char *directory = NULL;
+	char *mountpoint = "";
+	int android = 0;
+	int gzip = 0;
+	int sparse = 0;
+	int crc = 0;
+	int wipe = 0;
+	int init_itabs = 0;
 
-        while ((opt = getopt(argc, argv, "l:j:b:g:i:I:L:a:fwzJsct")) != -1) {
-                switch (opt) {
-                case 'l':
-                        info.len = parse_num(optarg);
-                        break;
-                case 'j':
-                        info.journal_blocks = parse_num(optarg);
-                        break;
-                case 'b':
-                        info.block_size = parse_num(optarg);
-                        break;
-                case 'g':
-                        info.blocks_per_group = parse_num(optarg);
-                        break;
-                case 'i':
-                        info.inodes = parse_num(optarg);
-                        break;
-                case 'I':
-                        info.inode_size = parse_num(optarg);
-                        break;
-                case 'L':
-                        info.label = optarg;
-                        break;
-                case 'f':
-                        force = 1;
-                        break;
-                case 'a':
-                        android = 1;
-                        mountpoint = optarg;
-                        break;
-                case 'w':
-                        wipe = 1;
-                        break;
-                case 'z':
-                        gzip = 1;
-                        break;
+	while ((opt = getopt(argc, argv, "l:j:b:g:i:I:L:a:fwzJsct")) != -1) {
+		switch (opt) {
+		case 'l':
+			info.len = parse_num(optarg);
+			break;
+		case 'j':
+			info.journal_blocks = parse_num(optarg);
+			break;
+		case 'b':
+			info.block_size = parse_num(optarg);
+			break;
+		case 'g':
+			info.blocks_per_group = parse_num(optarg);
+			break;
+		case 'i':
+			info.inodes = parse_num(optarg);
+			break;
+		case 'I':
+			info.inode_size = parse_num(optarg);
+			break;
+		case 'L':
+			info.label = optarg;
+			break;
+		case 'f':
+			force = 1;
+			break;
+		case 'a':
+			android = 1;
+			mountpoint = optarg;
+			break;
+		case 'w':
+			wipe = 1;
+			break;
+		case 'z':
+			gzip = 1;
+			break;
 		case 'J':
 			info.no_journal = 1;
 			break;
 		case 'c':
 			crc = 1;
 			break;
-                case 's':
-                        sparse = 1;
-                        break;
-                case 't':
-                        init_itabs = 1;
-                        break;
-                default: /* '?' */
-                        usage(argv[0]);
-                        exit(EXIT_FAILURE);
-                }
-        }
-
-	if (gzip && sparse) {
-                fprintf(stderr, "Cannot specify both gzip and sparse\n");
-                usage(argv[0]);
-                exit(EXIT_FAILURE);
+		case 's':
+			sparse = 1;
+			break;
+		case 't':
+			init_itabs = 1;
+			break;
+		default: /* '?' */
+			usage(argv[0]);
+			exit(EXIT_FAILURE);
+		}
 	}
 
-        if (wipe && sparse) {
-                fprintf(stderr, "Cannot specifiy both wipe and sparse\n");
-                usage(argv[0]);
-                exit(EXIT_FAILURE);
-        }
+	if (gzip && sparse) {
+		fprintf(stderr, "Cannot specify both gzip and sparse\n");
+		usage(argv[0]);
+		exit(EXIT_FAILURE);
+	}
 
-        if (wipe && gzip) {
-                fprintf(stderr, "Cannot specifiy both wipe and gzip\n");
-                usage(argv[0]);
-                exit(EXIT_FAILURE);
-        }
+	if (wipe && sparse) {
+		fprintf(stderr, "Cannot specifiy both wipe and sparse\n");
+		usage(argv[0]);
+		exit(EXIT_FAILURE);
+	}
 
-        if (optind >= argc) {
-                fprintf(stderr, "Expected filename after options\n");
-                usage(argv[0]);
-                exit(EXIT_FAILURE);
-        }
+	if (wipe && gzip) {
+		fprintf(stderr, "Cannot specifiy both wipe and gzip\n");
+		usage(argv[0]);
+		exit(EXIT_FAILURE);
+	}
 
-        filename = argv[optind++];
+	if (optind >= argc) {
+		fprintf(stderr, "Expected filename after options\n");
+		usage(argv[0]);
+		exit(EXIT_FAILURE);
+	}
 
-        if (optind < argc)
-                directory = argv[optind++];
+	filename = argv[optind++];
 
-        if (optind < argc) {
-                fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
-                usage(argv[0]);
-                exit(EXIT_FAILURE);
-        }
+	if (optind < argc)
+		directory = argv[optind++];
 
-        return make_ext4fs_internal(filename, directory, mountpoint, android, gzip,
-                       sparse, crc, wipe, init_itabs);
+	if (optind < argc) {
+		fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
+		usage(argv[0]);
+		exit(EXIT_FAILURE);
+	}
+
+	return make_ext4fs_internal(filename, directory, mountpoint, android, gzip,
+			sparse, crc, wipe, init_itabs);
 }
diff --git a/ext4_utils/output_file.c b/ext4_utils/output_file.c
index 39a6007..7b44020 100644
--- a/ext4_utils/output_file.c
+++ b/ext4_utils/output_file.c
@@ -355,6 +355,7 @@
 	zero_buf = malloc(info.block_size);
 	if (!zero_buf) {
 		error_errno("malloc zero_buf");
+		free(out);
 		return NULL;
 	}
 	memset(zero_buf, '\0', info.block_size);
@@ -446,7 +447,7 @@
 void write_data_block(struct output_file *out, u64 off, u8 *data, int len)
 {
 	int ret;
-	
+
 	if (off + len > (u64) info.len) {
 		error("attempted to write block %llu past end of filesystem",
 				off + len - info.len);
@@ -547,11 +548,7 @@
 			goto err;
 	}
 
-	munmap(data, len);
-
-	close(file_fd);
-
 err:
-	munmap(data, len);
+	munmap(data, len + aligned_diff);
 	close(file_fd);
 }
diff --git a/ext4_utils/simg2img.c b/ext4_utils/simg2img.c
index 228d289..12b92eb 100644
--- a/ext4_utils/simg2img.c
+++ b/ext4_utils/simg2img.c
@@ -247,7 +247,7 @@
 			fprintf(stderr, "Error reading chunk header\n");
 			exit(-1);
 		}
- 
+
 		if (sparse_header.chunk_hdr_sz > CHUNK_HEADER_LEN) {
 			/* Skip the remaining bytes in a header that is longer than
 			 * we expected.
diff --git a/libpagemap/include/pagemap/pagemap.h b/libpagemap/include/pagemap/pagemap.h
index 09ff29d..25c6161 100644
--- a/libpagemap/include/pagemap/pagemap.h
+++ b/libpagemap/include/pagemap/pagemap.h
@@ -129,10 +129,10 @@
                              unsigned long low, unsigned long hi,
                              uint64_t **range_out, size_t *len);
 
-#define _BITS(x, offset, bits) (((x) >> offset) & ((1LL << ((bits) + 1)) - 1))
+#define _BITS(x, offset, bits) (((x) >> offset) & ((1LL << (bits)) - 1))
 
 #define PM_PAGEMAP_PRESENT(x)     (_BITS(x, 63, 1))
-#define PM_PAGEMAP_SWAPPED(x)     (!_BITS(x, 62, 1))
+#define PM_PAGEMAP_SWAPPED(x)     (_BITS(x, 62, 1))
 #define PM_PAGEMAP_SHIFT(x)       (_BITS(x, 55, 6))
 #define PM_PAGEMAP_PFN(x)         (_BITS(x, 0, 55))
 #define PM_PAGEMAP_SWAP_OFFSET(x) (_BITS(x, 5, 50))
diff --git a/libpagemap/pm_map.c b/libpagemap/pm_map.c
index f683ba6..2d5c2f9 100644
--- a/libpagemap/pm_map.c
+++ b/libpagemap/pm_map.c
@@ -83,10 +83,6 @@
     pm_memusage_zero(&ws);
     
     for (i = 0; i < len; i++) {
-        if (!PM_PAGEMAP_PRESENT(pagemap[i]) ||
-            PM_PAGEMAP_SWAPPED(pagemap[i]))
-            continue;
-
         error = pm_kernel_flags(map->proc->ker, PM_PAGEMAP_PFN(pagemap[i]),
                                 &flags);
         if (error) goto out;
@@ -99,6 +95,7 @@
         if (error) goto out;
 
         ws.vss += map->proc->ker->pagesize;
+        if( PM_PAGEMAP_SWAPPED(pagemap[i]) ) continue;
         ws.rss += (count >= 1) ? (map->proc->ker->pagesize) : (0);
         ws.pss += (count >= 1) ? (map->proc->ker->pagesize / count) : (0);
         ws.uss += (count == 1) ? (map->proc->ker->pagesize) : (0);
diff --git a/libpagemap/pm_process.c b/libpagemap/pm_process.c
index b3c077e..dddff01 100644
--- a/libpagemap/pm_process.c
+++ b/libpagemap/pm_process.c
@@ -122,7 +122,7 @@
         free(range);
         *range_out = NULL;
         return 0;
-    } else if (error < 0 || (error > 0 && error < numpages * sizeof(uint64_t))) {
+    } else if (error < 0 || (error > 0 && error < (int)(numpages * sizeof(uint64_t)))) {
         error = (error < 0) ? errno : -1;
         free(range);
         return error;
@@ -210,7 +210,7 @@
 }
 
 #define INITIAL_MAPS 10
-#define MAX_LINE 256
+#define MAX_LINE 1024
 #define MAX_PERMS 5
 
 /*