toolbox: newfs_msdos: Exit on write failure, and handle bogus geometry from the kernel

Signed-off-by: San Mehat <san@google.com>
diff --git a/toolbox/newfs_msdos.c b/toolbox/newfs_msdos.c
index 779150e..49042c4 100644
--- a/toolbox/newfs_msdos.c
+++ b/toolbox/newfs_msdos.c
@@ -745,8 +745,10 @@
 	    }
 	    if ((n = write(fd, img, bpb.bps)) == -1)
 		err(1, "%s", fname);
-	    if ((unsigned)n != bpb.bps)
+	    if ((unsigned)n != bpb.bps) {
 		errx(1, "%s: can't write sector %u", fname, lsn);
+                exit(1);
+            }
 	}
     }
     return 0;
@@ -828,6 +830,16 @@
         geom.sectors = 63;
     }
 
+    if (!geom.heads) {
+        printf("Bogus heads from kernel - setting sane value\n");
+        geom.heads = 64;
+    }
+
+    if (!geom.sectors) {
+        printf("Bogus sectors from kernel - setting sane value\n");
+        geom.sectors = 63;
+    }
+
     bpb->spt = geom.sectors;
     ckgeom(fname, bpb->spt, "sectors/track");