/***************************************************************************************** Copyright (c) 2002-2004 The UbixOS Project All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions, the following disclaimer and the list of authors. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, the following disclaimer and the list of authors in the documentation and/or other materials provided with the distribution. Neither the name of the UbixOS Project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. $Id$ *****************************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include "ubixfs.h" #include "superblock.h" static void usage() { fprintf(stderr, "usage: format [-q] disk [slice]\n"); exit (1); } int main(int argc,char **argv) { FILE *fd; struct ubixDiskLabel *d = (struct ubixDiskLabel *)malloc(512); ubixfs_superBlock *sb = (ubixfs_superBlock *)malloc(sizeof(ubixfs_superBlock)); int ch = 0x0; int i = 0x0; int x = 0x0; short batSize = 0x0; u_int32_t blocks = 0x0; u_int32_t batSect = 0x0; char sector[512]; char q = 0x0; while ((ch = getopt(argc, argv, "q")) != -1) { switch (ch) { case 'q': q = 0x1; break; default: usage(); } } argc -= optind; argv += optind; if (argc < 1) usage(); printf("Ubix Disk Format Utility Version 1.0\n"); printf("(c) 2004 Ubix Corp \n\n"); fd = fopen(argv[0],"rb+"); if (fd == NULL) { fprintf(stderr,"Error: unable to open disk: %s\n",argv[0]); exit(1); } fseek(fd,512,0); fread(d,512,1,fd); if (d->magicNum != UBIX_LABEL_MAGIC || d->magicNum2 != UBIX_LABEL_MAGIC) { fprintf(stderr, "Error: invalid disk label\n"); exit(1); } i = atoi(argv[1]) - 1; if (i < 0) { fprintf(stderr, "Error: invalid partition: %s\n",argv[1]); exit(1); } if (d->partitions[i].p_fstype != UBIX_UBIXFS_MAGIC) { fprintf(stderr, "Error: not an ubix parition\n"); exit(1); } if (q == 0x0) { memset(sector,0x0,512); if (fseek(fd,d->partitions[i].p_offset * 512,0x0) != 0x0) { fprintf(stderr, "Error: fseek failed\n"); exit(0x1); } for (x = 1;x <= d->partitions[i].p_size;x++) { if (fwrite(sector,512,1,fd) != 1) { printf("Error: %i\n",x); exit(0x1); } } } blocks = (d->partitions[i].p_size - 1) / 8; batSize = (d->partitions[i].p_size - 1) % 8; while ((batSize * 4096) < blocks) { batSize += 0x4; blocks--; } batSect = (d->partitions[i].p_offset + 1) + (blocks * 8); /* write super block here */ if (fseek(fd,d->partitions[i].p_offset * 512,0x0) != 0x0) { fprintf(stderr, "Error: fseek failed\n"); exit(0x1); } fwrite(sb,512,1,fd); /* Write BAT */ if (fseek(fd,batSect * 512,0x0) != 0x0) { fprintf(stderr, "Error: fseek failed\n"); exit(0x1); } memset(sector,0x69,512); fwrite(sector,512,batSize,fd); /* Print debug info */ printf("blocks: [%i]\n",blocks); printf("batSect: [%i]\n",batSect); printf("batSize: [%i]\n",batSize); printf("d->partitions[%i].p_offset = %i\n",i,d->partitions[i].p_offset); printf("d->partitions[%i].p_size = %i\n",i,d->partitions[i].p_size); printf("d->partitions[%i].p_bsize = 0x%X\n",i,d->partitions[i].p_bsize); fclose(fd); printf("Format Complete\n"); free(d); free(sb); return(0); } /*** $Log$ Revision 1.2 2004/09/03 08:15:09 reddawg ok Revision 1.1 2004/09/01 09:26:13 reddawg frame works are laid out END ***/