Fix -Wsign-compare trouble

Mostly by changing to `unsigned` types where applicable, sometimes
`signed` types, and casting as a last resort.

Change-Id: I08895543ffb7a48058bcf91ef6500ca113f2d305
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/30409
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Jacob Garber <jgarber1@ualberta.ca>
diff --git a/82802ab.c b/82802ab.c
index 1046da4..281b66e 100644
--- a/82802ab.c
+++ b/82802ab.c
@@ -126,7 +126,7 @@
 /* chunksize is 1 */
 int write_82802ab(struct flashctx *flash, const uint8_t *src, unsigned int start, unsigned int len)
 {
-	int i;
+	unsigned int i;
 	chipaddr dst = flash->virtual_memory + start;
 
 	for (i = 0; i < len; i++) {
@@ -144,7 +144,7 @@
 {
 	chipaddr bios = flash->virtual_memory;
 	uint8_t mcfg, bcfg, need_unlock = 0, can_unlock = 0;
-	int i;
+	unsigned int i;
 
 	/* Clear status register */
 	chip_writeb(flash, 0x50, bios);
@@ -197,7 +197,7 @@
 	chipaddr bios = flash->virtual_memory;
 	uint8_t mcfg, bcfg;
 	uint8_t need_unlock = 0, can_unlock = 0;
-	int i;
+	unsigned int i;
 
 	/* Wait if chip is busy */
 	wait_82802ab(flash);
diff --git a/at45db.c b/at45db.c
index 38c0685..5f949bb 100644
--- a/at45db.c
+++ b/at45db.c
@@ -463,9 +463,9 @@
 	}
 
 	/* Create a suitable buffer to store opcode, address and data chunks for buffer1. */
-	const int max_data_write = flash->mst->spi.max_data_write - 4;
-	const unsigned int max_chunk = (max_data_write > 0 && max_data_write <= page_size) ?
-				       max_data_write : page_size;
+	const unsigned int max_data_write = flash->mst->spi.max_data_write;
+	const unsigned int max_chunk = max_data_write > 4 && max_data_write - 4 <= page_size ?
+				       max_data_write - 4 : page_size;
 	uint8_t buf[4 + max_chunk];
 
 	buf[0] = AT45DB_BUFFER1_WRITE;
diff --git a/bitbang_spi.c b/bitbang_spi.c
index 2dad149..7c183c3 100644
--- a/bitbang_spi.c
+++ b/bitbang_spi.c
@@ -150,7 +150,7 @@
 				    const unsigned char *writearr,
 				    unsigned char *readarr)
 {
-	int i;
+	unsigned int i;
 	const struct bitbang_spi_master *master = flash->mst->spi.data;
 
 	/* FIXME: Run bitbang_spi_request_bus here or in programmer init?
diff --git a/board_enable.c b/board_enable.c
index 5876036..74612f5 100644
--- a/board_enable.c
+++ b/board_enable.c
@@ -373,7 +373,8 @@
 
 static const struct winbond_chip *winbond_superio_chipdef(void)
 {
-	int i, j;
+	int i;
+	unsigned int j;
 
 	for (i = 0; i < superio_count; i++) {
 		if (superios[i].vendor != SUPERIO_VENDOR_WINBOND)
diff --git a/buspirate_spi.c b/buspirate_spi.c
index fb066c2..08cd04c 100644
--- a/buspirate_spi.c
+++ b/buspirate_spi.c
@@ -76,7 +76,8 @@
 static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt,
 			      unsigned int readcnt)
 {
-	int i, ret = 0;
+	unsigned int i;
+	int ret = 0;
 
 	msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
 	if (!writecnt && !readcnt) {
diff --git a/cbtable.c b/cbtable.c
index fe0c368..bdf53ce 100644
--- a/cbtable.c
+++ b/cbtable.c
@@ -34,7 +34,7 @@
  *	-1	if IDs in the image do not match the IDs embedded in the current firmware,
  *	 0	if the IDs could not be found in the image or if they match correctly.
  */
-int cb_check_image(const uint8_t *image, int size)
+int cb_check_image(const uint8_t *image, unsigned int size)
 {
 	const unsigned int *walk;
 	unsigned int mb_part_offset, mb_vendor_offset;
@@ -138,10 +138,10 @@
 		((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
 		rec = (struct lb_record *)(((char *)rec) + rec->size))
 
-static int count_lb_records(struct lb_header *head)
+static unsigned int count_lb_records(struct lb_header *head)
 {
 	struct lb_record *rec;
-	int count;
+	unsigned int count;
 
 	count = 0;
 	for_each_lbrec(head, rec) {
diff --git a/cli_classic.c b/cli_classic.c
index 0591bfe..b4112fd 100644
--- a/cli_classic.c
+++ b/cli_classic.c
@@ -507,7 +507,7 @@
 
 	for (j = 0; j < registered_master_count; j++) {
 		startchip = 0;
-		while (chipcount < ARRAY_SIZE(flashes)) {
+		while (chipcount < (int)ARRAY_SIZE(flashes)) {
 			startchip = probe_flash(&registered_masters[j], startchip, &flashes[chipcount], 0);
 			if (startchip == -1)
 				break;
diff --git a/dediprog.c b/dediprog.c
index 3566109..fbd6930 100644
--- a/dediprog.c
+++ b/dediprog.c
@@ -474,7 +474,7 @@
 		return 1;
 
 	int ret = dediprog_write(CMD_READ, value, idx, data_packet, sizeof(data_packet));
-	if (ret != sizeof(data_packet)) {
+	if (ret != (int)sizeof(data_packet)) {
 		msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, libusb_error_name(ret));
 		return 1;
 	}
@@ -487,7 +487,7 @@
 
 	/* Allocate bulk transfers. */
 	unsigned int i;
-	for (i = 0; i < min(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
+	for (i = 0; i < MIN(DEDIPROG_ASYNC_TRANSFERS, count); ++i) {
 		transfers[i] = libusb_alloc_transfer(0);
 		if (!transfers[i]) {
 			msg_perr("Allocating libusb transfer %i failed: %s!\n", i, libusb_error_name(ret));
@@ -630,7 +630,7 @@
 	if (prepare_rw_cmd(flash, data_packet, count, dedi_spi_cmd, &value, &idx, start, 0))
 		return 1;
 	int ret = dediprog_write(CMD_WRITE, value, idx, data_packet, sizeof(data_packet));
-	if (ret != sizeof(data_packet)) {
+	if (ret != (int)sizeof(data_packet)) {
 		msg_perr("Command Write SPI Bulk failed, %s!\n", libusb_error_name(ret));
 		return 1;
 	}
@@ -743,7 +743,7 @@
 		value = 0;
 	}
 	ret = dediprog_write(CMD_TRANSCEIVE, value, idx, writearr, writecnt);
-	if (ret != writecnt) {
+	if (ret != (int)writecnt) {
 		msg_perr("Send SPI failed, expected %i, got %i %s!\n",
 			 writecnt, ret, libusb_error_name(ret));
 		return 1;
@@ -770,7 +770,7 @@
 	ret = dediprog_read(CMD_TRANSCEIVE, value, idx, readarr, readcnt);
 	*/
 	ret = dediprog_read(CMD_TRANSCEIVE, 0, 0, readarr, readcnt);
-	if (ret != readcnt) {
+	if (ret != (int)readcnt) {
 		msg_perr("Receive SPI failed, expected %i, got %i %s!\n", readcnt, ret, libusb_error_name(ret));
 		return 1;
 	}
@@ -804,7 +804,7 @@
 	int sfnum;
 	int fw[3];
 	if (sscanf(buf, "SF%d V:%d.%d.%d ", &sfnum, &fw[0], &fw[1], &fw[2]) != 4 ||
-	    sfnum != dediprog_devicetype) {
+	    sfnum != (int)dediprog_devicetype) {
 		msg_perr("Unexpected firmware version string '%s'\n", buf);
 		return 1;
 	}
diff --git a/dmi.c b/dmi.c
index ae90f7c..9ec935a 100644
--- a/dmi.c
+++ b/dmi.c
@@ -147,7 +147,7 @@
 
 static void dmi_chassis_type(uint8_t code)
 {
-	int i;
+	unsigned int i;
 	code &= 0x7f; /* bits 6:0 are chassis type, 7th bit is the lock bit */
 	is_laptop = 2;
 	for (i = 0; i < ARRAY_SIZE(dmi_chassis_types); i++) {
@@ -161,7 +161,7 @@
 
 static void dmi_table(uint32_t base, uint16_t len, uint16_t num)
 {
-	int i = 0, j = 0;
+	unsigned int i = 0, j = 0;
 
 	uint8_t *dmi_table_mem = physmap_ro("DMI Table", base, len);
 	if (dmi_table_mem == NULL) {
@@ -346,7 +346,7 @@
 
 static int dmi_fill(void)
 {
-	int i;
+	unsigned int i;
 	char *chassis_type;
 
 	msg_pdbg("Using External DMI decoder.\n");
@@ -376,7 +376,7 @@
 
 static int dmi_shutdown(void *data)
 {
-	int i;
+	unsigned int i;
 	for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
 		free(dmi_strings[i].value);
 		dmi_strings[i].value = NULL;
@@ -406,7 +406,7 @@
 	}
 
 	has_dmi_support = 1;
-	int i;
+	unsigned int i;
 	for (i = 0; i < ARRAY_SIZE(dmi_strings); i++) {
 		msg_pdbg("DMI string %s: \"%s\"\n", dmi_strings[i].keyword,
 			 (dmi_strings[i].value == NULL) ? "" : dmi_strings[i].value);
@@ -463,7 +463,7 @@
 
 int dmi_match(const char *pattern)
 {
-	int i;
+	unsigned int i;
 
 	if (!has_dmi_support)
 		return 0;
diff --git a/dummyflasher.c b/dummyflasher.c
index 8f3dfda..0caffc9 100644
--- a/dummyflasher.c
+++ b/dummyflasher.c
@@ -58,8 +58,8 @@
 static unsigned int emu_jedec_ce_c7_size = 0;
 static unsigned char spi_blacklist[256];
 static unsigned char spi_ignorelist[256];
-static int spi_blacklist_size = 0;
-static int spi_ignorelist_size = 0;
+static unsigned int spi_blacklist_size = 0;
+static unsigned int spi_ignorelist_size = 0;
 static uint8_t emu_status = 0;
 
 /* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
@@ -151,7 +151,7 @@
 {
 	char *bustext = NULL;
 	char *tmp = NULL;
-	int i;
+	unsigned int i;
 #if EMULATE_SPI_CHIP
 	char *status = NULL;
 #endif
@@ -231,7 +231,7 @@
 		msg_pdbg("SPI blacklist is ");
 		for (i = 0; i < spi_blacklist_size; i++)
 			msg_pdbg("%02x ", spi_blacklist[i]);
-		msg_pdbg(", size %i\n", spi_blacklist_size);
+		msg_pdbg(", size %u\n", spi_blacklist_size);
 	}
 	free(tmp);
 
@@ -267,7 +267,7 @@
 		msg_pdbg("SPI ignorelist is ");
 		for (i = 0; i < spi_ignorelist_size; i++)
 			msg_pdbg("%02x ", spi_ignorelist[i]);
-		msg_pdbg(", size %i\n", spi_ignorelist_size);
+		msg_pdbg(", size %u\n", spi_ignorelist_size);
 	}
 	free(tmp);
 
@@ -819,7 +819,7 @@
 				  const unsigned char *writearr,
 				  unsigned char *readarr)
 {
-	int i;
+	unsigned int i;
 
 	msg_pspew("%s:", __func__);
 
diff --git a/en29lv640b.c b/en29lv640b.c
index 550642d..5b01904 100644
--- a/en29lv640b.c
+++ b/en29lv640b.c
@@ -29,7 +29,7 @@
 /* chunksize is 1 */
 int write_en29lv640b(struct flashctx *flash, const uint8_t *src, unsigned int start, unsigned int len)
 {
-	int i;
+	unsigned int i;
 	chipaddr bios = flash->virtual_memory;
 	chipaddr dst = flash->virtual_memory + start;
 
diff --git a/flash.h b/flash.h
index b60a980..c1d8980 100644
--- a/flash.h
+++ b/flash.h
@@ -301,7 +301,11 @@
 
 /* helpers.c */
 uint32_t address_to_bits(uint32_t addr);
-int bitcount(unsigned long a);
+unsigned int bitcount(unsigned long a);
+#undef MIN
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#undef MAX
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
 int max(int a, int b);
 int min(int a, int b);
 char *strcat_realloc(char *dest, const char *src);
diff --git a/flashrom.c b/flashrom.c
index cb1dca6..06b1854 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -1253,7 +1253,7 @@
 		ret = 1;
 		goto out;
 	}
-	if (image_stat.st_size != size) {
+	if (image_stat.st_size != (intmax_t)size) {
 		msg_gerr("Error: Image size (%jd B) doesn't match the flash chip's size (%lu B)!\n",
 			 (intmax_t)image_stat.st_size, size);
 		ret = 1;
diff --git a/fmap.c b/fmap.c
index 31f282f..ca25651 100644
--- a/fmap.c
+++ b/fmap.c
@@ -96,7 +96,7 @@
 	off_t offset;
 	bool fmap_found = 0;
 
-	for (offset = 0; offset <= len - sizeof(struct fmap); offset++) {
+	for (offset = 0; offset <= (off_t)(len - sizeof(struct fmap)); offset++) {
 		if (is_valid_fmap((struct fmap *)&buf[offset])) {
 			fmap_found = 1;
 			break;
@@ -178,7 +178,7 @@
 }
 
 static int fmap_bsearch_rom(struct fmap **fmap_out, struct flashctx *const flashctx,
-		size_t rom_offset, size_t len, int min_stride)
+		size_t rom_offset, size_t len, size_t min_stride)
 {
 	size_t stride, fmap_len = 0;
 	int ret = 1, fmap_found = 0, check_offset_0 = 1;
diff --git a/helpers.c b/helpers.c
index a714908..cfa9812 100644
--- a/helpers.c
+++ b/helpers.c
@@ -30,9 +30,9 @@
 	return 32 - lzb;
 }
 
-int bitcount(unsigned long a)
+unsigned int bitcount(unsigned long a)
 {
-	int i = 0;
+	unsigned int i = 0;
 	for (; a != 0; a >>= 1)
 		if (a & 1)
 			i++;
diff --git a/ich_descriptors.c b/ich_descriptors.c
index a757add..684b845 100644
--- a/ich_descriptors.c
+++ b/ich_descriptors.c
@@ -35,10 +35,6 @@
 #include "flash.h" /* for msg_* */
 #include "programmer.h"
 
-#ifndef min
-#define min(a, b) (((a) < (b)) ? (a) : (b))
-#endif
-
 ssize_t ich_number_of_regions(const enum ich_chipset cs, const struct ich_desc_content *const cont)
 {
 	switch (cs) {
@@ -365,7 +361,7 @@
 
 void prettyprint_ich_descriptor_region(const enum ich_chipset cs, const struct ich_descriptors *const desc)
 {
-	size_t i;
+	ssize_t i;
 	const ssize_t nr = ich_number_of_regions(cs, &desc->content);
 	msg_pdbg2("=== Region Section ===\n");
 	if (nr < 0) {
@@ -374,18 +370,18 @@
 		return;
 	}
 	for (i = 0; i < nr; i++)
-		msg_pdbg2("FLREG%zu   0x%08x\n", i, desc->region.FLREGs[i]);
+		msg_pdbg2("FLREG%zd   0x%08x\n", i, desc->region.FLREGs[i]);
 	msg_pdbg2("\n");
 
 	msg_pdbg2("--- Details ---\n");
 	for (i = 0; i < nr; i++)
-		pprint_freg(&desc->region, i);
+		pprint_freg(&desc->region, (uint32_t)i);
 	msg_pdbg2("\n");
 }
 
 void prettyprint_ich_descriptor_master(const enum ich_chipset cs, const struct ich_descriptors *const desc)
 {
-	size_t i;
+	ssize_t i;
 	const ssize_t nm = ich_number_of_masters(cs, &desc->content);
 	msg_pdbg2("=== Master Section ===\n");
 	if (nm < 0) {
@@ -394,7 +390,7 @@
 		return;
 	}
 	for (i = 0; i < nm; i++)
-		msg_pdbg2("FLMSTR%zu  0x%08x\n", i + 1, desc->master.FLMSTRs[i]);
+		msg_pdbg2("FLMSTR%zd  0x%08x\n", i + 1, desc->master.FLMSTRs[i]);
 	msg_pdbg2("\n");
 
 	msg_pdbg2("--- Details ---\n");
@@ -402,7 +398,7 @@
 		const char *const master_names[] = {
 			"BIOS", "ME", "GbE", "unknown", "EC",
 		};
-		if (nm >= ARRAY_SIZE(master_names)) {
+		if (nm >= (ssize_t)ARRAY_SIZE(master_names)) {
 			msg_pdbg2("%s: number of masters too high (%d).\n", __func__,
 				  desc->content.NM + 1);
 			return;
@@ -423,7 +419,7 @@
 			"BIOS", "ME", "GbE", "DE", "BMC", "IE",
 		};
 		/* NM starts at 1 instead of 0 for LBG */
-		if (nm > ARRAY_SIZE(master_names)) {
+		if (nm > (ssize_t)ARRAY_SIZE(master_names)) {
 			msg_pdbg2("%s: number of masters too high (%d).\n", __func__,
 				  desc->content.NM);
 			return;
@@ -446,14 +442,14 @@
 		}
 	} else if (cs == CHIPSET_APOLLO_LAKE) {
 		const char *const master_names[] = { "BIOS", "TXE", };
-		if (nm > ARRAY_SIZE(master_names)) {
+		if (nm > (ssize_t)ARRAY_SIZE(master_names)) {
 			msg_pdbg2("%s: number of masters too high (%d).\n", __func__, desc->content.NM);
 			return;
 		}
 
 		msg_pdbg2("       FD   IFWI  TXE   n/a  Platf DevExp\n");
 		for (i = 0; i < nm; i++) {
-			size_t j;
+			ssize_t j;
 			msg_pdbg2("%-4s", master_names[i]);
 			for (j = 0; j < ich_number_of_regions(cs, &desc->content); j++)
 				msg_pdbg2("   %c%c ",
@@ -799,7 +795,7 @@
 	unsigned int i, max_count;
 	msg_pdbg2("=== Softstraps ===\n");
 
-	max_count = min(ARRAY_SIZE(desc->north.STRPs), desc->content.MSL);
+	max_count = MIN(ARRAY_SIZE(desc->north.STRPs), desc->content.MSL);
 	if (max_count < desc->content.MSL) {
 		msg_pdbg2("MSL (%u) is greater than the current maximum of %u entries.\n",
 			  desc->content.MSL, max_count);
@@ -811,7 +807,7 @@
 		msg_pdbg2("STRP%-2d = 0x%08x\n", i, desc->north.STRPs[i]);
 	msg_pdbg2("\n");
 
-	max_count = min(ARRAY_SIZE(desc->south.STRPs), desc->content.ISL);
+	max_count = MIN(ARRAY_SIZE(desc->south.STRPs), desc->content.ISL);
 	if (max_count < desc->content.ISL) {
 		msg_pdbg2("ISL (%u) is greater than the current maximum of %u entries.\n",
 			  desc->content.ISL, max_count);
@@ -965,8 +961,8 @@
 int read_ich_descriptors_from_dump(const uint32_t *const dump, const size_t len,
 				   enum ich_chipset *const cs, struct ich_descriptors *const desc)
 {
-	size_t i, max_count;
-	uint8_t pch_bug_offset = 0;
+	ssize_t i, max_count;
+	size_t pch_bug_offset = 0;
 
 	if (dump == NULL || desc == NULL)
 		return ICH_RET_PARAM;
@@ -1000,14 +996,14 @@
 
 	/* region */
 	const ssize_t nr = ich_number_of_regions(*cs, &desc->content);
-	if (nr < 0 || len < getFRBA(&desc->content) + nr * 4)
+	if (nr < 0 || len < getFRBA(&desc->content) + (size_t)nr * 4)
 		return ICH_RET_OOB;
 	for (i = 0; i < nr; i++)
 		desc->region.FLREGs[i] = dump[(getFRBA(&desc->content) >> 2) + i];
 
 	/* master */
 	const ssize_t nm = ich_number_of_masters(*cs, &desc->content);
-	if (nm < 0 || len < getFMBA(&desc->content) + nm * 4)
+	if (nm < 0 || len < getFMBA(&desc->content) + (size_t)nm * 4)
 		return ICH_RET_OOB;
 	for (i = 0; i < nm; i++)
 		desc->master.FLMSTRs[i] = dump[(getFMBA(&desc->content) >> 2) + i];
@@ -1034,7 +1030,7 @@
 		return ICH_RET_OOB;
 
 	/* limit the range to be written */
-	max_count = min(sizeof(desc->north.STRPs) / 4, desc->content.MSL);
+	max_count = MIN(sizeof(desc->north.STRPs) / 4, desc->content.MSL);
 	for (i = 0; i < max_count; i++)
 		desc->north.STRPs[i] = dump[(getFMSBA(&desc->content) >> 2) + i];
 
@@ -1043,7 +1039,7 @@
 		return ICH_RET_OOB;
 
 	/* limit the range to be written */
-	max_count = min(sizeof(desc->south.STRPs) / 4, desc->content.ISL);
+	max_count = MIN(sizeof(desc->south.STRPs) / 4, desc->content.ISL);
 	for (i = 0; i < max_count; i++)
 		desc->south.STRPs[i] = dump[(getFISBA(&desc->content) >> 2) + i];
 
@@ -1134,7 +1130,7 @@
 
 int read_ich_descriptors_via_fdo(enum ich_chipset cs, void *spibar, struct ich_descriptors *desc)
 {
-	size_t i;
+	ssize_t i;
 	struct ich_desc_region *r = &desc->region;
 
 	/* Test if bit-fields are working as expected.
@@ -1226,7 +1222,8 @@
 	memset(layout, 0x00, sizeof(*layout));
 
 	ssize_t i, j;
-	for (i = 0, j = 0; i < min(ich_number_of_regions(cs, &desc.content), ARRAY_SIZE(regions)); ++i) {
+	const ssize_t nr = MIN(ich_number_of_regions(cs, &desc.content), (ssize_t)ARRAY_SIZE(regions));
+	for (i = 0, j = 0; i < nr; ++i) {
 		const chipoff_t base = ICH_FREG_BASE(desc.region.FLREGs[i]);
 		const chipoff_t limit = ICH_FREG_LIMIT(desc.region.FLREGs[i]);
 		if (limit <= base)
diff --git a/ichspi.c b/ichspi.c
index e1ede60..8b8f0f6 100644
--- a/ichspi.c
+++ b/ichspi.c
@@ -470,7 +470,7 @@
 
 static uint8_t lookup_spi_type(uint8_t opcode)
 {
-	int a;
+	unsigned int a;
 
 	for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
 		if (POSSIBLE_OPCODES[a].opcode == opcode)
@@ -1564,7 +1564,7 @@
 	"locked", "read-only", "write-only", "read-write"
 };
 
-static enum ich_access_protection ich9_handle_frap(uint32_t frap, int i)
+static enum ich_access_protection ich9_handle_frap(uint32_t frap, unsigned int i)
 {
 	const int rwperms_unknown = ARRAY_SIZE(access_names);
 	static const char *const region_names[6] = {
@@ -1595,23 +1595,23 @@
 	limit = ICH_FREG_LIMIT(freg);
 	if (base > limit || (freg == 0 && i > 0)) {
 		/* this FREG is disabled */
-		msg_pdbg2("0x%02X: 0x%08x FREG%i: %s region is unused.\n",
+		msg_pdbg2("0x%02X: 0x%08x FREG%u: %s region is unused.\n",
 			  offset, freg, i, region_name);
 		return NO_PROT;
 	}
 	msg_pdbg("0x%02X: 0x%08x ", offset, freg);
 	if (rwperms == 0x3) {
-		msg_pdbg("FREG%i: %s region (0x%08x-0x%08x) is %s.\n", i,
+		msg_pdbg("FREG%u: %s region (0x%08x-0x%08x) is %s.\n", i,
 			 region_name, base, limit, access_names[rwperms]);
 		return NO_PROT;
 	}
 	if (rwperms == rwperms_unknown) {
-		msg_pdbg("FREG%i: %s region (0x%08x-0x%08x) has unknown permissions.\n",
+		msg_pdbg("FREG%u: %s region (0x%08x-0x%08x) has unknown permissions.\n",
 			 i, region_name, base, limit);
 		return NO_PROT;
 	}
 
-	msg_pinfo("FREG%i: %s region (0x%08x-0x%08x) is %s.\n", i,
+	msg_pinfo("FREG%u: %s region (0x%08x-0x%08x) is %s.\n", i,
 		  region_name, base, limit, access_names[rwperms]);
 	return access_perms_to_protection[rwperms];
 }
@@ -1625,7 +1625,7 @@
 #define ICH_PR_PERMS(pr)	(((~((pr) >> PR_RP_OFF) & 1) << 0) | \
 				 ((~((pr) >> PR_WP_OFF) & 1) << 1))
 
-static enum ich_access_protection ich9_handle_pr(const size_t reg_pr0, int i)
+static enum ich_access_protection ich9_handle_pr(const size_t reg_pr0, unsigned int i)
 {
 	uint8_t off = reg_pr0 + (i * 4);
 	uint32_t pr = mmio_readl(ich_spibar + off);
@@ -1701,7 +1701,7 @@
 
 int ich_init_spi(void *spibar, enum ich_chipset ich_gen)
 {
-	int i;
+	unsigned int i;
 	uint16_t tmp2;
 	uint32_t tmp;
 	char *arg;
@@ -1786,7 +1786,7 @@
 		for (i = 0; i < 3; i++) {
 			int offs;
 			offs = 0x60 + (i * 4);
-			msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
+			msg_pdbg("0x%02x: 0x%08x (PBR%u)\n", offs,
 				     mmio_readl(ich_spibar + offs), i);
 		}
 		if (mmio_readw(ich_spibar) & (1 << 15)) {
diff --git a/it85spi.c b/it85spi.c
index 3cd5514..5ce9193 100644
--- a/it85spi.c
+++ b/it85spi.c
@@ -327,7 +327,7 @@
 				   const unsigned char *writearr,
 				   unsigned char *readarr)
 {
-	int i;
+	unsigned int i;
 
 	it85xx_enter_scratch_rom();
 	/* Exit scratch ROM ONLY when programmer shuts down. Otherwise, the
diff --git a/it87spi.c b/it87spi.c
index f8b2b0f..f5955fe 100644
--- a/it87spi.c
+++ b/it87spi.c
@@ -281,7 +281,6 @@
 				    unsigned char *readarr)
 {
 	uint8_t busy, writeenc;
-	int i;
 
 	do {
 		busy = INB(it8716f_flashport) & 0x80;
@@ -330,6 +329,8 @@
 		| ((readcnt & 0x3) << 2) | (writeenc), it8716f_flashport);
 
 	if (readcnt > 0) {
+		unsigned int i;
+
 		do {
 			busy = INB(it8716f_flashport) & 0x80;
 		} while (busy);
diff --git a/jedec.c b/jedec.c
index ac1ea0d..0709efa 100644
--- a/jedec.c
+++ b/jedec.c
@@ -408,7 +408,8 @@
 int write_jedec_1(struct flashctx *flash, const uint8_t *src, unsigned int start,
 		  unsigned int len)
 {
-	int i, failed = 0;
+	unsigned int i;
+	int failed = 0;
 	chipaddr dst = flash->virtual_memory + start;
 	chipaddr olddst;
 	unsigned int mask;
@@ -430,7 +431,8 @@
 static int write_page_write_jedec_common(struct flashctx *flash, const uint8_t *src,
 					 unsigned int start, unsigned int page_size)
 {
-	int i, tried = 0, failed;
+	unsigned int i;
+	int tried = 0, failed;
 	const uint8_t *s = src;
 	chipaddr bios = flash->virtual_memory;
 	chipaddr dst = bios + start;
diff --git a/layout.c b/layout.c
index 6e476c2..d80b01f 100644
--- a/layout.c
+++ b/layout.c
@@ -46,7 +46,8 @@
 	struct flashrom_layout *const layout = get_global_layout();
 	FILE *romlayout;
 	char tempstr[256], tempname[256];
-	int i, ret = 1;
+	unsigned int i;
+	int ret = 1;
 
 	romlayout = fopen(name, "r");
 
@@ -154,7 +155,7 @@
  */
 int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args)
 {
-	int found = 0;
+	unsigned int found = 0;
 	const struct layout_include_args *tmp;
 
 	if (args == NULL)
@@ -193,7 +194,7 @@
 void layout_cleanup(struct layout_include_args **args)
 {
 	struct flashrom_layout *const layout = get_global_layout();
-	int i;
+	unsigned int i;
 	struct layout_include_args *tmp;
 
 	while (*args) {
@@ -216,7 +217,7 @@
 	chipsize_t total_size = flash->chip->total_size * 1024;
 	int ret = 0;
 
-	int i;
+	unsigned int i;
 	for (i = 0; i < layout->num_entries; i++) {
 		if (layout->entries[i].start >= total_size || layout->entries[i].end >= total_size) {
 			msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's "
diff --git a/pickit2_spi.c b/pickit2_spi.c
index 52021d9..784dc13 100644
--- a/pickit2_spi.c
+++ b/pickit2_spi.c
@@ -209,7 +209,7 @@
 	}
 
 	uint8_t buf[CMD_LENGTH] = {CMD_DOWNLOAD_DATA, writecnt};
-	int i = 2;
+	unsigned int i = 2;
 	for (; i < writecnt + 2; i++) {
 		buf[i] = writearr[i - 2];
 	}
diff --git a/print.c b/print.c
index cfe6267..2901c50 100644
--- a/print.c
+++ b/print.c
@@ -19,6 +19,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <stddef.h>
 #include "flash.h"
 #include "programmer.h"
 
@@ -314,20 +315,20 @@
 #if CONFIG_INTERNAL == 1
 static void print_supported_chipsets(void)
 {
-	int i, chipsetcount = 0;
+	unsigned int i, chipsetcount = 0;
 	const struct penable *c = chipset_enables;
-	int maxvendorlen = strlen("Vendor") + 1;
-	int maxchipsetlen = strlen("Chipset") + 1;
+	size_t maxvendorlen = strlen("Vendor") + 1;
+	size_t maxchipsetlen = strlen("Chipset") + 1;
 
 	for (c = chipset_enables; c->vendor_name != NULL; c++) {
 		chipsetcount++;
-		maxvendorlen = max(maxvendorlen, strlen(c->vendor_name));
-		maxchipsetlen = max(maxchipsetlen, strlen(c->device_name));
+		maxvendorlen = MAX(maxvendorlen, strlen(c->vendor_name));
+		maxchipsetlen = MAX(maxchipsetlen, strlen(c->device_name));
 	}
 	maxvendorlen++;
 	maxchipsetlen++;
 
-	msg_ginfo("Supported chipsets (total: %d):\n\n", chipsetcount);
+	msg_ginfo("Supported chipsets (total: %u):\n\n", chipsetcount);
 
 	msg_ginfo("Vendor");
 	for (i = strlen("Vendor"); i < maxvendorlen; i++)
@@ -354,12 +355,12 @@
 static void print_supported_boards_helper(const struct board_info *boards,
 				   const char *devicetype)
 {
-	int i;
+	unsigned int i;
 	unsigned int boardcount_good = 0, boardcount_bad = 0, boardcount_nt = 0;
 	const struct board_match *e = board_matches;
 	const struct board_info *b = boards;
-	int maxvendorlen = strlen("Vendor") + 1;
-	int maxboardlen = strlen("Board") + 1;
+	size_t maxvendorlen = strlen("Vendor") + 1;
+	size_t maxboardlen = strlen("Board") + 1;
 
 	for (b = boards; b->vendor != NULL; b++) {
 		maxvendorlen = max(maxvendorlen, strlen(b->vendor));
diff --git a/print_wiki.c b/print_wiki.c
index ea8c4d7..b2ac65c 100644
--- a/print_wiki.c
+++ b/print_wiki.c
@@ -245,7 +245,7 @@
 					   "<span id=\"%s_note%d\">%d. [[#%s_ref%d|&#x2191;]]</span>"
 					   " <nowiki>%s</nowiki><br />\n", devicetype, num_notes, num_notes,
 					   devicetype, num_notes, boards[i].note);
-			if (ret < 0 || ret >= sizeof(tmp)) {
+			if (ret < 0 || (unsigned int)ret >= sizeof(tmp)) {
 				fprintf(stderr, "Footnote text #%d of %s truncated (ret=%d, sizeof(tmp)=%zu)\n",
 					num_notes, devicetype, ret, sizeof(tmp));
 			}
diff --git a/programmer.h b/programmer.h
index f4e8b46..dfa6ebd 100644
--- a/programmer.h
+++ b/programmer.h
@@ -304,7 +304,7 @@
 
 /* cbtable.c */
 int cb_parse_table(const char **vendor, const char **model);
-int cb_check_image(const uint8_t *bios, int size);
+int cb_check_image(const uint8_t *bios, unsigned int size);
 
 /* dmi.c */
 #if defined(__i386__) || defined(__x86_64__)
diff --git a/sb600spi.c b/sb600spi.c
index fd9487c..3026836 100644
--- a/sb600spi.c
+++ b/sb600spi.c
@@ -247,7 +247,7 @@
 
 	reset_internal_fifo_pointer();
 	msg_pspew("Filling FIFO: ");
-	int count;
+	unsigned int count;
 	for (count = 0; count < writecnt; count++) {
 		msg_pspew("[%02x]", writearr[count]);
 		mmio_writeb(writearr[count], sb600_spibar + 0xC);
@@ -326,7 +326,7 @@
 	mmio_writeb(readcnt, sb600_spibar + 0x4b);
 
 	msg_pspew("Filling buffer: ");
-	int count;
+	unsigned int count;
 	for (count = 0; count < writecnt; count++) {
 		msg_pspew("[%02x]", writearr[count]);
 		mmio_writeb(writearr[count], sb600_spibar + 0x80 + count);
diff --git a/serial.c b/serial.c
index e61df74..3a99dbf 100644
--- a/serial.c
+++ b/serial.c
@@ -480,10 +480,10 @@
 	}
 #endif
 
-	int i;
-	int rd_bytes = 0;
+	unsigned int i;
+	unsigned int rd_bytes = 0;
 	for (i = 0; i < timeout; i++) {
-		msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes);
+		msg_pspew("readcnt %u rd_bytes %u\n", readcnt, rd_bytes);
 #if IS_WINDOWS
 		ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL);
 		msg_pspew("read %lu bytes\n", rv);
@@ -560,10 +560,10 @@
 	}
 #endif
 
-	int i;
-	int wr_bytes = 0;
+	unsigned int i;
+	unsigned int wr_bytes = 0;
 	for (i = 0; i < timeout; i++) {
-		msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes);
+		msg_pspew("writecnt %u wr_bytes %u\n", writecnt, wr_bytes);
 #if IS_WINDOWS
 		WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL);
 		msg_pspew("wrote %lu bytes\n", rv);
diff --git a/serprog.c b/serprog.c
index b6c85c9..37a9db4 100644
--- a/serprog.c
+++ b/serprog.c
@@ -71,8 +71,8 @@
 static uint32_t sp_write_n_bytes = 0;
 
 /* sp_streamed_* used for flow control checking */
-static int sp_streamed_transmit_ops = 0;
-static int sp_streamed_transmit_bytes = 0;
+static unsigned int sp_streamed_transmit_ops = 0;
+static unsigned int sp_streamed_transmit_bytes = 0;
 
 /* sp_opbuf_usage used for counting the amount of
 	on-device operation buffer used */
diff --git a/sst28sf040.c b/sst28sf040.c
index 9df27b9..3da25f1 100644
--- a/sst28sf040.c
+++ b/sst28sf040.c
@@ -75,7 +75,7 @@
 /* chunksize is 1 */
 int write_28sf040(struct flashctx *flash, const uint8_t *src, unsigned int start, unsigned int len)
 {
-	int i;
+	unsigned int i;
 	chipaddr bios = flash->virtual_memory;
 	chipaddr dst = flash->virtual_memory + start;
 
diff --git a/sst_fwhub.c b/sst_fwhub.c
index c712303..d688a35 100644
--- a/sst_fwhub.c
+++ b/sst_fwhub.c
@@ -21,7 +21,7 @@
 #include "flash.h"
 #include "chipdrivers.h"
 
-static int check_sst_fwhub_block_lock(struct flashctx *flash, int offset)
+static int check_sst_fwhub_block_lock(struct flashctx *flash, unsigned int offset)
 {
 	chipaddr registers = flash->virtual_registers;
 	uint8_t blockstatus;
@@ -47,7 +47,7 @@
 	return blockstatus & 0x1;
 }
 
-static int clear_sst_fwhub_block_lock(struct flashctx *flash, int offset)
+static int clear_sst_fwhub_block_lock(struct flashctx *flash, unsigned int offset)
 {
 	chipaddr registers = flash->virtual_registers;
 	uint8_t blockstatus;
@@ -67,7 +67,7 @@
 
 int printlock_sst_fwhub(struct flashctx *flash)
 {
-	int i;
+	unsigned int i;
 
 	for (i = 0; i < flash->chip->total_size * 1024; i += flash->chip->page_size)
 		check_sst_fwhub_block_lock(flash, i);
@@ -77,7 +77,8 @@
 
 int unlock_sst_fwhub(struct flashctx *flash)
 {
-	int i, ret=0;
+	unsigned int i;
+	int ret = 0;
 
 	for (i = 0; i < flash->chip->total_size * 1024; i += flash->chip->page_size)
 	{
diff --git a/usbblaster_spi.c b/usbblaster_spi.c
index 886cb57..d6b5084 100644
--- a/usbblaster_spi.c
+++ b/usbblaster_spi.c
@@ -119,11 +119,11 @@
 
 static int send_write(unsigned int writecnt, const unsigned char *writearr)
 {
-	int i;
 	uint8_t buf[BUF_SIZE];
 
 	memset(buf, 0, sizeof(buf));
 	while (writecnt) {
+		unsigned int i;
 		unsigned int n_write = min(writecnt, BUF_SIZE - 1);
 		msg_pspew("writing %d-byte packet\n", n_write);
 
diff --git a/wbsio_spi.c b/wbsio_spi.c
index 28b568e..f4f6af9 100644
--- a/wbsio_spi.c
+++ b/wbsio_spi.c
@@ -115,7 +115,7 @@
 				  const unsigned char *writearr,
 				  unsigned char *readarr)
 {
-	int i;
+	unsigned int i;
 	uint8_t mode = 0;
 
 	msg_pspew("%s:", __func__);