/***************************************************************************************** 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" #include "inode.h" #include "btreeheader.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; bTreeHeader * bth = (bTreeHeader *)malloc(4096); 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],"wb+"); 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,SEEK_SET) != 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 + (blocks * 8)); /* write super block here */ rewind(fd); if (fseek(fd,((d->partitions[i].p_offset + d->partitions[i].p_size - 1) * 512),SEEK_SET) != 0x0) { fprintf(stderr, "Error: fseek failed for super block\n"); exit(0x1); } // fill in superblock here memset(sb, 0, sizeof(ubixfs_superBlock)); strcpy(sb->name, "UbixFS"); sb->magic1 = UBIX_MAGIC1; sb->fsByteOrder = 0; sb->blockSize = 4096; sb->blockShift = 12; sb->numBlocks = blocks; sb->usedBlocks = 1; // superblock counts as a used block sb->inodeCount = 0; sb->inodeSize = 4096; sb->magic2 = UBIX_MAGIC2; sb->blocksPerAG = 2048; sb->AGShift = 11; sb->numAGs = (sb->numBlocks+2047) / 2048; sb->lastUsedAG = 0; sb->flags = 0x434C454E; // CLEN sb->logBlocks.allocationGroup = 0; sb->logBlocks.start = 0; sb->logBlocks.len = 0; sb->logStart = 0; sb->logEnd = 0; sb->magic3 = UBIX_MAGIC3; sb->indicies.allocationGroup = 0; sb->indicies.start = 0; sb->indicies.len = 0; sb->rootDir.allocationGroup = 2; sb->rootDir.start = 0; sb->rootDir.len = 1; if (fwrite(sb, 512, 1, fd) != 0x1) { fprintf(stderr, "Error: Could not write out super block\n"); exit(0x1); } // write out the root dir memset(bth, sb->blockSize, 0); bth->firstDeleted = -1; rewind(fd); fseek(fd, sb->rootDir.allocationGroup * sb->blockSize * sb->blocksPerAG, SEEK_SET); fwrite(bth, 4096, 1, fd); /* Write BAT */ rewind(fd); if (fseek(fd,batSect * 512,SEEK_SET) != 0x0) { fprintf(stderr, "Error: fseek failed for bat\n"); exit(0x1); } memset(sector, 0x0 ,512); sector[0] = 0x80; if (fwrite(sector,512,1,fd) != 1) { fprintf(stderr, "Error: %i\n",x); exit(0x1); } sector[0] = 0x0; for (x = 1;x < batSize;x++) { if (fwrite(sector,512,1,fd) != 1) { fprintf(stderr, "Error: %i\n",x); exit(0x1); } } /* 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.10 2004/09/06 17:52:49 flameshadow add: UBIX_MAGIC numbers for the superblock Revision 1.9 2004/09/06 16:44:21 flameshadow chg: changed the pointers in include/inode.h to be void * add: wrote out root dir in the format utility Revision 1.8 2004/09/06 10:24:56 flameshadow chg: sync Revision 1.7 2004/09/03 14:05:41 reddawg Fixed format to move super block Revision 1.6 2004/09/03 13:33:00 flameshadow chg: BAT should be empty before allocating space for the root dir, indicies, and journal Revision 1.5 2004/09/03 12:30:56 flameshadow chg: used original/new superblock definition add: most of the superblock info filled in for format Revision 1.4 2004/09/03 11:22:54 flameshadow Note: beat ubu over the head because his internet connection is terrible Revision 1.3 2004/09/03 09:24:27 reddawg Disk Tools Work 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 ***/