/***********************************************************************
 *
 * Function: cmd_ls
 *
 * Purpose: Displays files in root directory of block device
 *
 * Processing:
 *     See function.
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Always returns TRUE
 *
 * Notes: None
 *
 **********************************************************************/
BOOL_32 cmd_ls(void) 
{
	UNS_8 dirname [32];
	BOOL_32 lp;

	/* Initialize FAT interface first */
	if (fat_init() == FALSE)
	{
		term_dat_out(blkdeverr_msg);
	}
	else
	{
		/* Show items in BLKDEV root directory */
		lp = fat_get_dir(dirname, TRUE);
		while (lp == FALSE) 
		{
			term_dat_out(" ");
			term_dat_out_crlf(dirname);
			lp = fat_get_dir(dirname, FALSE);
		}

		fat_deinit();
	}

	return TRUE;
}
Пример #2
0
void vfs_init() {
	DEBUG_MSG(("Setting up VFS"));
	filesystems = linked_list_map_create();
	mounts = linked_list_map_create();

	file_handles = kmalloc(sizeof(file_handles_t) * VFS_FILE_HANDLES);

	fat_init();

	DEBUG_MSG(("Setting up VFS"));
}
Пример #3
0
int vfs_init( void )
{
	// initilize Device File System driver
	dfs_init();
	// mount the DFS
	vfs_mount( NULL, "/amos/device/", DFS_TYPE );

	// initilize FAT File System driver
	fat_init();

	return SUCCESS;
}
Пример #4
0
void fat_start(void)
{
  char filenamestr[20];
  char filenamelogstr[20];
  char console_text[50];
  fat_dirent de_root;
  fat_dirent user_dump;
  float fw = ((float)ciGetRevision())/100.0f;
  u32 device_magic = ciGetMagic();
  u32 is64drive = (ciGetMagic() == 0x55444556);

  if(fw < 2.00 || fw > 20.00 || !is64drive){
    draw_puts("\n    - ERROR WRITING TO MEMORY CARD:\n    64DRIVE with FIRMWARE 2.00 or later is required!");
	while(1);
  }
  
  sprintf(console_text, "\n    - 64drive with firmware %.2f found", fw);
  draw_puts(console_text);
  
  sprintf(filenamestr, "64DD_IPL.n64");

  sprintf(console_text, "\n    - Writing %s to memory card", filenamestr);
  draw_puts(console_text);
  
  // get FAT going
  if(fat_init() != 0){
    fail = 100;
    fs_fail();
    return;
  }
  
  // start in root directory
  fat_root_dirent(&de_root);
  if(fail != 0){
    fs_fail();
    return;
  }
  
  
  if( fat_find_create(filenamestr, &de_root, &user_dump, 0, 1) != 0){
    sprintf(fat_message, "Failed to create image dump"); fail = 3;
    fs_fail(); return;
  }

  if( fat_set_size(&user_dump, 0x400000) != 0) {
    sprintf(fat_message, "Failed to resize dump"); fail = 4;
    fs_fail(); return;
  }

  loadRamToRom(0x0, user_dump.start_cluster);
}
Пример #5
0
void init_thread(void* param1, void* param2)
{
    ahci_init();
    //ahci_test();
    fat_init();
    pe_init();

    //LoadSysModule("sys/test.sys");
    //for(int i=0; i <16; i++)
	process_create("sys/init.exe");

    while(1)
    {
        mdelay(2000);
        printk("1");
    }
}
Пример #6
0
void ds2_main(void)
{
	int err;
ds2_setCPUclocklevel(13);
	//Initial video and audio and other input and output
	err = ds2io_initb(512, 22050, 0, 0);
	if(err) goto _failure;

	//Initial file system
	err = fat_init();
	if(err) goto _failure;

	//go to user main funtion
	sfc_main (0, 0);

_failure:
	ds2_plug_exit();
}
Пример #7
0
void cmd_init(void)
{
	/* SD Karte Initilisieren */
	if (mmc_init())
	{
		uart_puts_P("Searching for SD/MMC  [Error]\r\n");
		uart_puts_P("mmc_init()            [Error]\r\n");
		uart_puts_P("Skipping! You MUST call init later on.\r\n");
	}
	else
	{
		uart_puts_P("Searching for SD/MMC  [OK]\r\n");
		uart_puts_P("mmc_init()            [OK]\r\n");

		fat_init(); // laden Cluster OFFSET und Size
		uart_puts_P("fat_init()            [OK]\r\n");
	}
}
Пример #8
0
void ds2_main(void)
{
    int err;
    HighFrequencyCPU();
    //Initial video and audio and other input and output
    err = ds2io_initb(DS2_BUFFER_SIZE, SND_SAMPLE_RATE, 0, 0);
    if(err) goto _failure;

    //Initial file system
    err = fat_init();
    if(err) goto _failure;

    //go to user main funtion
    sfc_main (0, 0);

_failure:
    ds2_plug_exit();
}
Пример #9
0
/** @brief Init OS after load.
 *  @param NULL
 *  @return NULL
 */
void OS_Init(void)
{
	BOOTINFO* info;
	
	int32 screenWidth, screenHeight;
	uint8* bgBuf, *resVram;
	boolean re[2];
	uint32 tmpAddr;

	InitPeripheralBuffer();
	
	init_gdtidt();
	init_pic();
	io_sti();		// 恢复中断
	tim_init();
	io_out8(PIC0_IMR, 0xf8); /* PIT和PIC1和键盘设置为许可(11111000) */
	io_out8(PIC1_IMR, 0xef); /* 鼠标设置为许可(11101111) */
	
	info = (BOOTINFO*) ADR_BOOTINFO;
	screenHeight = info->screenHeight;
	screenWidth = info->screenWidth;
	
	init_palette();
	mem_init();
	fat_init();
	re[0] = mem_alloc(768 * 1024, &tmpAddr);
	resVram = (uint8*)tmpAddr;
	sheet_init(resVram);
	
	InitMouse();
	InitKeyboard();
	
	re[1] = mem_alloc(768 * 1024, &tmpAddr);
	bgBuf = (uint8*)tmpAddr;
	
	//screenWidth * screenHeight
	sheet_add(bgBuf, screenWidth, screenHeight, 0, 0, NONE_COL, &bgSheet);

	fill_box(bgSheet, DARKGRASS, 0, 0, info->screenWidth, info->screenHeight);
	if(re[0] == FALSE || re[1] == FALSE) {
		fill_box(bgSheet, RED, 0, 0, info->screenWidth, info->screenHeight);
	}
}
Пример #10
0
int try_sd_load(void)
{
	int err;

	err = sd_init();
	if (err) {
		printf("SD card not found (%d)\n", err);
		return err;
	}

	err = fat_init();
	if (err == 0)
		printf("SD card detected\n");
	else {
		printf("SD card not detected (%d)\n", err);
		return err;
	}

//	if (usbgecko_checkgecko())
//		printf("USBGecko serial interface detected\n");
//	else
//		printf("USBGecko serial interface not detected\n");

	printf("Opening boot.elf:\n");
	err = fat_open("boot.elf");

	if (err) {
		printf("boot.elf not found (%d)\n", err);
		return err;
	}

extern u32 fat_file_size;

	printf("reading %d bytes...\n", fat_file_size);
	err = fat_read(code_buffer, fat_file_size);
	if (err) {
		printf("Error %d reading file\n", err);
		return err;
	}

	printf("Done.\n");
	return 0;
}
Пример #11
0
int init_file_op(int fs_type){

if( fs_type == PARTITION_FAT32 || PARTITION_FAT32_LBA ){

        fat_init();

	fs_op_ak.open =  fat_fopen ;
	fs_op_ak.read =  fat_fread ;
        fs_op_ak.close =  fat_fclose ;

}

if( fs_type == PARTITION_EXT2 ){

	 ext2fs_init_ak();

	fs_op_ak.open =  ext2_fopen;
	fs_op_ak.read =  ext2_fread ;
        fs_op_ak.close =  ext2_fclose ;
}

 return 0;

}
Пример #12
0
int _fat_open(struct vfs_file_s *file, struct mount_s *mount, const char *filename, int oflags, int perm)
{
	struct fat_context * fat = malloc(sizeof(struct fat_context));
	memset(fat, 0, sizeof(*fat));
	
	file->priv[0] = fat;
	struct bdev *bdev = mount->priv[0];

	if (fat_init(fat, bdev) < 0)
	{
		free(fat);
		return -1;
	}
	
	if (fat_open(fat, filename) < 0)
	{
		free(fat);
		return -1;
	}

	file->ops = &vfs_fat_ops;
	
	return 0;
}
Пример #13
0
void Main (void)
{
	void (*kernel_entry_point)(struct BootInfo *bi);
	void (*procman_entry_point)(void);
	size_t nbytes_read;
	size_t filesz;
	void *mod;
	void *addr;
	vm_addr heap_base, heap_ceiling;
	vm_addr user_stack_base, user_stack_ceiling;

    // Initialize bootloader
	
	dbg_init();
	BlinkLEDs(10);
	sd_card_init(&bdev);
	fat_init();
	init_mem();

	// Load kernel at 1MB mark
	
	if (elf_load ("BOOT/KERNEL", (void *)&kernel_entry_point) != 0)
		KPanic ("Cannot load kernel");
	
	kernel_phdr_cnt = phdr_cnt;
	bmemcpy (kernel_phdr_table, phdr_table, sizeof (Elf32_PHdr) *  MAX_PHDR);
	
	user_base = 0x00800000;
	heap_base    = segment_table[segment_cnt-1].base;
	heap_ceiling = segment_table[segment_cnt-1].ceiling;
		
	if (heap_ceiling > user_base)
		heap_ceiling = user_base;
	
	addr = SegmentCreate (heap_base, heap_ceiling - heap_base, SEG_TYPE_ALLOC, MAP_FIXED | PROT_READWRITE);

	if (addr == NULL)
		KPanic ("Could not allocate HEAP!");

	KLog ("heap_base = %#010x, heap_ceiling = %#010x", heap_base, heap_ceiling);

    
    // Load root process (the Executive at 8MB mark
		
	if (elf_load ("BOOT/FILESYS", (void *)&procman_entry_point) != 0)
		KPanic ("Cannot load filesystem manager");

	procman_phdr_cnt = phdr_cnt;
	bmemcpy (procman_phdr_table, phdr_table, sizeof (Elf32_PHdr) * MAX_PHDR);


    // Allocate memory for module table, read startup.txt
    // and load modules above 8MB
    
	module_table = SegmentCreate (user_base, sizeof (struct Module) * NMODULE, SEG_TYPE_ALLOC, PROT_READWRITE);
	KLog ("module_table = %#010x", module_table);

	if ((vm_addr)module_table < user_base)
		KPanic ("Bad module_table");
	
	if (fat_open ("BOOT/STARTUP.TXT") != 0)
		KPanic ("Cannot open STARTUP.TXT");
	
	module_cnt = 0;
	
	while (module_cnt < NMODULE)
	{
		if ((nbytes_read = fat_getline(module_table[module_cnt].name, 64)) == 0)
			break;
		
		if (module_table[module_cnt].name[0] == '\0' ||
			module_table[module_cnt].name[0] == '\n' ||
			module_table[module_cnt].name[0] == '\r' ||
			module_table[module_cnt].name[0] == ' ')
		{
			KLOG ("Skipping blank lines");
			continue;
		}
		
		if (fat_open (module_table[module_cnt].name) != 0)
			KPanic ("Cannot open module");
		
		filesz = fat_get_filesize();
		
		mod = SegmentCreate (user_base, filesz, SEG_TYPE_ALLOC, PROT_READWRITE);
		
		if (fat_read (mod, filesz) == filesz)
		{
			module_table[module_cnt].addr = mod;
			module_table[module_cnt].size = filesz;
			module_cnt++;
		}
	}
	
	
	// Allocate a stack for the Executive
	
	user_stack_base = SegmentCreate (user_base, 65536, SEG_TYPE_ALLOC, PROT_READWRITE);	
	user_stack_ceiling = user_stack_base + 65536;

    
    // Pass the bootinfo, segment_table and module_table to kernel.
    
	bootinfo.procman_entry_point = procman_entry_point;
	bootinfo.user_stack_base = user_stack_base;
	bootinfo.user_stack_ceiling = user_stack_ceiling;
	bootinfo.user_base = user_base;
	bootinfo.heap_base = heap_base;
	bootinfo.heap_ceiling = heap_ceiling;
	bootinfo.screen_width = screen_width;
	bootinfo.screen_height = screen_height;
	bootinfo.screen_buf = screen_buf;
	bootinfo.screen_pitch = screen_pitch;
	bootinfo.module_table = module_table;
	bootinfo.module_cnt = module_cnt;
	bootinfo.segment_table = segment_table;
	bootinfo.segment_cnt = segment_cnt;
		
	KLOG ("Calling KERNEL entry point %#010x", (uint32) kernel_entry_point);
	
	kernel_entry_point(&bootinfo);
	while (1);
}
Пример #14
0
int32
/* Start of GurumNetworks modification
TFFS_mount( IN  FileSystemDriver* driver, OUT tffs_handle_t * phtffs)
End of GurumNetworks modification*/
TFFS_mount( IN  FileSystemDriver* driver, int first_lba)
{
	int32 ret;
	tffs_t * tffs;
	boot_sector_t * pbs = NULL;
	DiskDriver* disk_driver = driver->driver;
	tdir_t * proot_dir = NULL;
	tdir_t * pcur_dir = NULL;
	tfat_t * pfat = NULL;
	uint32 rootdir_clus;
	void* read_buf = NULL;
	void* fat = NULL;

	/* Start of GurumNetworks modification
	tdev_handle_t hdev;
	tcache_t * pcache = NULL;
	End of GurumNetworks modification */

	/* Start of GurumNetworks modification	
	if (!dev || !phtffs)
		return ERR_TFFS_INVALID_PARAM;
	End of GurumNetworks modification */

	ret = TFFS_OK;
	tffs = (tffs_t *)malloc(sizeof(tffs_t));
	memset(tffs, 0, sizeof(tffs_t));
	pbs = (boot_sector_t *)gmalloc(sizeof(boot_sector_t));
	memset(pbs, 0, sizeof(boot_sector_t));

#ifdef _KERNEL_	
	ASSERT(sizeof(boot_sector_t) == 512);
#endif

	/* Start of GurumNetworks modification	
	hdev = HAI_initdevice(dev, 512);
	if (hdev == NULL) {
		ret = ERR_TFFS_DEVICE_FAIL;
		goto _release;
	}
	tffs->hdev = hdev;

	if (HAI_readsector(hdev, 0, (ubyte *)pbs) != HAI_OK) {
		ret = ERR_TFFS_DEVICE_FAIL;
		goto _release;
	}

	End of GurumNetworks modification */
	tffs->driver = driver;
	tffs->first_lba = first_lba;

	if(disk_driver->read(disk_driver, tffs->first_lba, 1, (void*)pbs) < 0)
		goto _release;

	tffs->pbs = pbs;
	
	if (!_validate_bs(pbs)) {
		ret = ERR_TFFS_BAD_BOOTSECTOR;
		goto _release;
	}

	_parse_boot_sector(pbs, tffs);
	DBG("tffs->fat_type:%d\n", tffs->fat_type);
	DBG("tffs->sec_fat:%d\n", tffs->sec_fat);
	DBG("tffs->sec_root_dir:%d\n", tffs->sec_root_dir);
	DBG("tffs->sec_first_data:%d\n", tffs->sec_first_data);

	if ((pfat = fat_init(tffs)) == NULL) {
		ret = ERR_TFFS_BAD_FAT;
		goto _release;
	}

	tffs->pfat = pfat;

	/* Start of GurumNetworks modification
	if ((pcache = cache_init(hdev, 32, tffs->pbs->byts_per_sec)) == NULL) {
		WARN("TFFS: cache is disable.\n");
		pcache = cache_init(hdev, 0, tffs->pbs->byts_per_sec);
	}
	tffs->pcache = pcache;
	End of GurumNetworks modification */

	rootdir_clus = tffs->fat_type == FT_FAT32 ? tffs->pbs->bh32.root_clus : ROOT_DIR_CLUS_FAT16;

	if (dir_init_by_clus(tffs, rootdir_clus, &proot_dir) != DIR_OK ||
		dir_init_by_clus(tffs, rootdir_clus, &pcur_dir) != DIR_OK) {
		ret = ERR_TFFS_DEVICE_FAIL;
		goto _release;
	}

	tffs->root_dir = proot_dir;
	tffs->cur_dir = pcur_dir;

	/* Start of GurumNetworks modification
	*phtffs = (tffs_handle_t)tffs;
	End of GurumNetworks modification */

	int i = 0;
	size_t read_size = 0;
	size_t fat_size = tffs->fatsz * pbs->byts_per_sec;
	read_buf = gmalloc(FS_BLOCK_SIZE);
	fat = bmalloc();

	while(read_size < fat_size) {
	#define MIN(x, y) (((x) < (y)) ? (x) : (y))
		if(disk_driver->read(disk_driver, tffs->sec_fat + i * FS_SECTOR_PER_BLOCK, FS_SECTOR_PER_BLOCK, read_buf) < 0) {
			ret = -99; // TODO: errno for fat error
			goto _release;
		}

		size_t copy_size = MIN(fat_size - read_size, FS_BLOCK_SIZE);
		memcpy((uint8_t*)fat + read_size, read_buf, copy_size);

		read_size += copy_size;
		i++;
	}

	tffs->fat = fat;
	DBG("tffs->fat:%p\n", tffs->fat);
	DBG("tffs->fat size:%d\n", tffs->fatsz);
	DBG("Tiny FAT file system mount OK.\n");
	driver->priv = tffs;

	gfree(read_buf);
	return ret;

_release:
	if (pfat)
		fat_destroy(pfat);
	if (proot_dir)
		dir_destroy(proot_dir);
	if (pcur_dir)
		dir_destroy(pcur_dir);
	if (HAI_closedevice(tffs->hdev) != HAI_OK)
		ret = ERR_TFFS_DEVICE_FAIL;

	if(read_buf)
		gfree(read_buf);
	if(fat)
		free(fat);

	free(pbs);
	free(tffs);

	return ret;
}
Пример #15
0
void main()
{
	int i;
	char *ptr;

	vdp_set_address(0x8004); // mode 4, disable hbl irq
	vdp_set_address(0x8160); // screen on, enable vbl irq
	vdp_set_address(0x820e); // name table @ $3800
	vdp_set_address(0x85ff); // sprite table @ $3f00
	vdp_set_address(0x8700); // backdrop is color 0
	vdp_set_address(0x8800); // scrollx is 0
	vdp_set_address(0x8900); // scrolly is 0

	vdp_set_address(0xc000);
	ptr = pal;
	for (i=0; i<32; i++) {
		vdp_write(*ptr++);
	}

	// turn off sprites
	vdp_set_address(0x3f00);
	vdp_write(0xd0);

	//Sound off - if resetted from a prior game
	set_sound_volume(0,0);
	set_sound_volume(1,0);
	set_sound_volume(2,0);
	set_sound_volume(3,0);

	while (1) {
		console_init();
		console_clear();

		console_gotoxy(3,0);
		console_puts("MASTER SYSTEM ROM LOADER\n");
		console_gotoxy(3,1);
		console_puts("------------------------\n");
		//console_gotoxy(3,3);

		i = 0;
		if (!sd_init()) {
			console_puts("Error initializing SD/MMC card\n");
		} else {
	#ifdef DEBUG2
			console_puts("SD card initialized\n");
	#endif
			if (!fat_init()) {
				//console_puts("could not initialize FAT system\n"); //qq
	
			} else {
	#ifdef DEBUG2
				console_puts("FAT system initialized\n");
	#endif
				i = 1;
			}
		}


		choose_mode(i);
	}
}
Пример #16
0
//int main(void) {
////main function
int main (void) {
  u32 waitForCard = 0;

  // set up avr32 hardware and peripherals
  init_avr32();

 
  print_dbg("\r\n SRAM size: 0x");
  print_dbg_hex(smc_get_cs_size(1));


  cpu_irq_disable();
  /// test the SRAM
  sram_test();

  cpu_irq_enable();

  //memory manager
  init_mem();
  print_dbg("\r\n init_mem");

  // wait for sdcard
  
    print_dbg("\r\n SD check... ");
    while (!sd_mmc_spi_mem_check()) {
      waitForCard++;
    }
    print_dbg("\r\nfound SD card. ");


    // intialize the FAT filesystem
    print_dbg("\r\n init fat");
    fat_init();
    // setup control logic
    print_dbg("\r\n init ctl");
    init_ctl();
  
    /* // initialize the application */
    /* app_init(); */
    /* print_dbg("\r\n init app"); */

    // initialize flash:
    firstrun = init_flash();
    print_dbg("r\n init flash, firstrun: ");
    print_dbg_ulong(firstrun);

    screen_startup();

    // find and load dsp from sdcard
    files_search_dsp();


    print_dbg("\r\n starting event loop.\r\n");

    // dont do startup
    startup = 0;

    while(1) {
      check_events();
    }
}
Пример #17
0
bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename)
{
	u32 sectors_per_fat;
	u32 sectors_per_disk;

	// Convert MB to bytes
	disk_size *= 1024 * 1024;

	if (disk_size < 0x800000 || disk_size > 0x800000000ULL)
	{
		ERROR_LOG(COMMON, "Trying to create SD Card image of size %" PRIu64 "MB is out of range (8MB-32GB)", disk_size / (1024 * 1024));
		return false;
	}

	// Pretty unlikely to overflow.
	sectors_per_disk = (u32)(disk_size / 512);
	sectors_per_fat = get_sectors_per_fat(disk_size, get_sectors_per_cluster(disk_size));

	boot_sector_init(s_boot_sector, s_fsinfo_sector, disk_size, nullptr);
	fat_init(s_fat_head);

	File::IOFile file(filename, "wb");
	FILE* const f = file.GetHandle();
	if (!f)
	{
		ERROR_LOG(COMMON, "Could not create file '%s', aborting...", filename.c_str());
		return false;
	}

	/* Here's the layout:
	*
	*  boot_sector
	*  fsinfo_sector
	*  empty
	*  backup boot sector
	*  backup fsinfo sector
	*  RESERVED_SECTORS - 4 empty sectors (if backup sectors), or RESERVED_SECTORS - 2 (if no backup)
	*  first fat
	*  second fat
	*  zero sectors
	*/

	if (write_sector(f, s_boot_sector))
		goto FailWrite;

	if (write_sector(f, s_fsinfo_sector))
		goto FailWrite;

	if (BACKUP_BOOT_SECTOR > 0)
	{
		if (write_empty(f, BACKUP_BOOT_SECTOR - 2))
			goto FailWrite;

		if (write_sector(f, s_boot_sector))
			goto FailWrite;

		if (write_sector(f, s_fsinfo_sector))
			goto FailWrite;

		if (write_empty(f, RESERVED_SECTORS - 2 - BACKUP_BOOT_SECTOR))
			goto FailWrite;
	}
	else
	{
		if (write_empty(f, RESERVED_SECTORS - 2)) goto FailWrite;
	}

	if (write_sector(f, s_fat_head))
		goto FailWrite;

	if (write_empty(f, sectors_per_fat - 1))
		goto FailWrite;

	if (write_sector(f, s_fat_head))
		goto FailWrite;

	if (write_empty(f, sectors_per_fat - 1))
		goto FailWrite;

	if (write_empty(f, sectors_per_disk - RESERVED_SECTORS - 2 * sectors_per_fat))
		goto FailWrite;

	return true;

FailWrite:
	ERROR_LOG(COMMON, "Could not write to '%s', aborting...", filename.c_str());
	if (unlink(filename.c_str()) < 0)
		ERROR_LOG(COMMON, "unlink(%s) failed: %s", filename.c_str(), GetLastErrorMsg().c_str());
	return false;
}
Пример #18
0
void _fat_mount(struct mount_s *mount, struct bdev * device)
{
	mount->priv[0] = device;
	fat_init();
	disk_mount(device->index,mount->index);
}
Пример #19
0
int read_mbr(struct block_device *parent, struct block_device ***partitions, int *part_count)
{
	(void)partitions;
	(void)part_count;
	(void)driver_name;
	/* Check the validity of the parent device */
	if(parent == (void*)0)
	{
		printf("MBR: invalid parent device\n");
		return -1;
	}

	/* Read the first 512 bytes */
	uint8_t *block_0 = (uint8_t *)malloc(512);
#ifdef MBR_DEBUG
	printf("MBR: reading block 0 from device %s\n", parent->device_name);
#endif
	
	int ret = block_read(parent, block_0, 512, 0);
	if(ret < 0)
	{
		printf("MBR: block_read failed (%i)\n", ret);
		return ret;
	}
	if(ret != 512)
	{
		printf("MBR: unable to read first 512 bytes of device %s, only %d bytes read\n",
				parent->device_name, ret);
		return -1;
	}

	/* Check the MBR signature */
	if((block_0[0x1fe] != 0x55) || (block_0[0x1ff] != 0xaa))
	{
		printf("MBR: no valid mbr signature on device %s (bytes are %x %x)\n",
				parent->device_name, block_0[0x1fe], block_0[0x1ff]);
		return -1;
	}
	printf("MBR: found valid MBR on device %s\n", parent->device_name);

#ifdef MBR_DEBUG
	/* Dump the first sector */
	printf("MBR: first sector:");
	for(int dump_idx = 0; dump_idx < 512; dump_idx++)
	{
		if((dump_idx & 0xf) == 0)
			printf("\n%03x: ", dump_idx);
		printf("%02x ", block_0[dump_idx]);
	}
	printf("\n");
#endif

	/* Load the partitions */
	struct block_device **parts =
		(struct block_device **)malloc(4 * sizeof(struct block_device *));
	int cur_p = 0;
	for(int i = 0; i < 4; i++)
	{
		int p_offset = 0x1be + (i * 0x10);
		if(block_0[p_offset + 4] != 0x00)
		{
			// Valid partition
			struct mbr_block_dev *d =
				(struct mbr_block_dev *)malloc(sizeof(struct mbr_block_dev));
			memset(d, 0, sizeof(struct mbr_block_dev));
			d->bd.driver_name = driver_name;
			char *dev_name = (char *)malloc(strlen(parent->device_name + 2));
			strcpy(dev_name, parent->device_name);
			dev_name[strlen(parent->device_name)] = '_';
			dev_name[strlen(parent->device_name) + 1] = '0' + i;
			dev_name[strlen(parent->device_name) + 2] = 0;
			d->bd.device_name = dev_name;
			d->bd.device_id = (uint8_t *)malloc(1);
			d->bd.device_id[0] = i;
			d->bd.dev_id_len = 1;
			d->bd.read = mbr_read;
			d->bd.block_size = 512;
			d->part_no = i;
			d->part_id = block_0[p_offset + 4];
			d->start_block = read_word(block_0, p_offset + 8);
			d->blocks = read_word(block_0, p_offset + 12);
			d->parent = parent;
			
			parts[cur_p++] = (struct block_device *)d;
#ifdef MBR_DEBUG
			printf("MBR: partition number %i (%s) of type %x, start sector %u, "
					"sector count %u, p_offset %03x\n", 
					d->part_no, d->bd.device_name, d->part_id,
					d->start_block, d->blocks, p_offset);
#endif

			switch(d->part_id)
			{
				case 1:
				case 4:
				case 6:
				case 0xb:
				case 0xc:
				case 0xe:
				case 0x11:
				case 0x14:
				case 0x1b:
				case 0x1c:
				case 0x1e:
					fat_init((struct block_device *)d, &d->bd.fs);
					break;

				case 0x83:
					ext2_init((struct block_device *)d, &d->bd.fs);
					break;
			}

			if(d->bd.fs)
				vfs_register(d->bd.fs);
		}
	}

	*partitions = parts;
	*part_count = cur_p;
	printf("MBR: found total of %i partition(s)\n", cur_p);

	return 0;
}
Пример #20
0
int  main( int argc, char**  argv )
{
    Wide   disk_size;
    int    sectors_per_fat;
    int    sectors_per_disk;
    char*  end;
    const char*  label = NULL;
    FILE*  f = NULL;

    for ( ; argc > 1 && argv[1][0] == '-'; argc--, argv++ )
    {
        char*  arg = argv[1] + 1;
        switch (arg[0]) {
            case 'l':
                if (arg[1] != 0)
                    arg += 2;
                else {
                    argc--;
                    argv++;
                    if (argc <= 1)
                        usage();
                    arg = argv[1];
                }
                label = arg;
                break;

            default:
                usage();
        }
    }

    if (argc != 3)
        usage();

    disk_size = strtoll( argv[1], &end, 10 );
    if (disk_size < 0 || (disk_size == 0 && (errno == EINVAL || errno == ERANGE))) {
        fprintf(stderr, "Invalid argument size '%s'\n\n", argv[1]);
        usage();
    }

    if (*end == 'K')
        disk_size *= 1024;
    else if (*end == 'M')
        disk_size *= 1024*1024;
    else if (*end == 'G')
        disk_size *= 1024*1024*1024;

    if (disk_size < 9*1024*1024) {
        fprintf(stderr, "Invalid argument: size '%s' is too small.\n\n", argv[1]);
        usage();
    } else if (disk_size > MAX_DISK_SIZE) {
        fprintf(stderr, "Invalid argument: size '%s' is too large.\n\n", argv[1]);
        usage();
    }

    sectors_per_disk = disk_size / BYTES_PER_SECTOR;
    sectors_per_fat  = get_sectors_per_fat( disk_size, get_sectors_per_cluster( disk_size ) );

    boot_sector_init( s_boot_sector, s_fsinfo_sector, disk_size, NULL );
    fat_init( s_fat_head );

    f = fopen( argv[2], "wb" );
    if ( !f ) {
      fprintf(stderr, "Could not create file '%s': %s\n", argv[2], strerror(errno));
      goto FailWrite;
    }

   /* here's the layout:
    *
    *  boot_sector
    *  fsinfo_sector
    *  empty
    *  backup boot sector
    *  backup fsinfo sector
    *  RESERVED_SECTORS - 4 empty sectors (if backup sectors), or RESERVED_SECTORS - 2 (if no backup)
    *  first fat
    *  second fat
    *  zero sectors
   */

    if ( write_sector( f, s_boot_sector ) ) goto FailWrite;
    if ( write_sector( f, s_fsinfo_sector ) ) goto FailWrite;
    if ( BACKUP_BOOT_SECTOR > 0 ) {
        if ( write_empty( f, BACKUP_BOOT_SECTOR - 2 ) ) goto FailWrite;
        if ( write_sector( f, s_boot_sector ) ) goto FailWrite;
        if ( write_sector( f, s_fsinfo_sector ) ) goto FailWrite;
        if ( write_empty( f, RESERVED_SECTORS - 2 - BACKUP_BOOT_SECTOR ) ) goto FailWrite;
    }
    else if ( write_empty( f, RESERVED_SECTORS - 2 ) ) goto FailWrite;

    if ( write_sector( f, s_fat_head ) ) goto FailWrite;
    if ( write_empty( f, sectors_per_fat-1 ) ) goto FailWrite;

    if ( write_sector( f, s_fat_head ) ) goto FailWrite;
    if ( write_empty( f, sectors_per_fat-1 ) ) goto FailWrite;

    if ( write_empty( f, sectors_per_disk - RESERVED_SECTORS - 2*sectors_per_fat ) ) goto FailWrite;

    fclose(f);
    return 0;

FailWrite:
    if (f != NULL) {
      fclose(f);
      unlink( argv[2] );
      fprintf(stderr, "File '%s' was not created.\n", argv[2]);
    }
    return 1;
}
Пример #21
0
// Kernel
void kernel(dword magic, dword addr) {
	dword * t;
	byte * buffer;
	multiboot_info_t * multiboot_info;
	// Print something
	clrscr();
	puts("Starting...\n");
	// MBoot check
	if(magic != MULTIBOOT_BOOTLOADER_MAGIC) {
		puts("Invalid magic number!\n");
		for(;;);
	}
	multiboot_info = (multiboot_info_t *)addr;
	if (CHECK_FLAG(multiboot_info -> flags, 4) && CHECK_FLAG(multiboot_info -> flags, 5)) {
        puts("Invalid! Both bits 4 and 5 are set!\n");
        for(;;);
	}
	printf("multiboot_info_flags = 0x%x\n", multiboot_info -> flags);
    if(CHECK_FLAG(multiboot_info -> flags, 0)) {
		printf("mem_lower = 0x%x KiB\n", multiboot_info -> mem_lower);
		printf("mem_upper = 0x%x KiB\n", multiboot_info -> mem_upper);
	}
	if(CHECK_FLAG(multiboot_info -> flags, 1)) {
		printf("boot_device = 0x%x\n", multiboot_info -> boot_device);
	}
	if(CHECK_FLAG(multiboot_info -> flags, 2)) {
		printf("cmdline = %s\n", (char *)multiboot_info -> cmdline);
	}
	// Init GDT
	set_gd(gd + 0, 0x0, 0x0, 0x0); // zero
	set_gd(gd + 1, 0x0, 0xcfffff, 0x9a); // cs
	set_gd(gd + 2, 0x0, 0xcfffff, 0x92); // ds
	gdtr.limit = 0x2000;
	gdtr.gd = gd;
	asm("lgdt %0"::"m"(gdtr));
	// Init IRQ
	init_irq();
	// Init IDT
	populate_idt(id);
	idtr.limit = 0x7ff;
	idtr.id = id;
	asm("lidt %0"::"m"(idtr));
	// Init paging
	init_paging();
	asm ("" : :"a"(pdbr));
	asm("mov %eax, %cr3");
	asm("mov %cr0, %eax");
	asm("bts $0x1f, %eax");
	asm("mov %eax, %cr0");
	asm("jmp . + 2");
	// Test FAT
	fat_init(& media_read, 30);
	buffer = malloc(512);
    fat_loadfile("test", buffer, 512);
	puts((char *)buffer);
	// Permit int
	asm("sti");
	// Test paging
	t = (dword *)0x900000;
	*t = 7;
	// LOOP
	for(;;);
}
Пример #22
0
/***********************************************************************
 *
 * Function: raw_load
 *
 * Purpose: Load a raw file from a source to memory
 *
 * Processing:
 *     See function.
 *
 * Parameters:
 *     fdata : Pointer to file data to fill
 *     addr  : Address to load data
 *     filename : Filename (sd cards only)
 *     src   : Data source
 *
 * Outputs: None
 *
 * Returns: TRUE if the file was loaded, otherwise FALSE
 *
 * Notes: None
 *
 **********************************************************************/
BOOL_32 raw_load(FILE_DATA_T *fdata,
                 UNS_32 addr,
                 UNS_8 *filename,
                 SRC_LOAD_T src) 
{
	UNS_32 rb, bytes = 0;
	UNS_8 ch, *ptr8 = (UNS_8 *) addr;
	BOOL_32 readloop, loaded = FALSE;

	if (src == SRC_TERM) 
	{
		term_dat_out_crlf(rawdl_msg);

		/* Read data from terminal until a break is encountered */
		while (term_break() == FALSE) 
		{
			if (term_dat_in(&ch, 1) > 0) 
			{
				bytes++;
				*ptr8 = ch;
				ptr8++;
			}
		}

		fdata->num_bytes = bytes;
		fdata->contiguous = TRUE;
		loaded = TRUE;
	}
	else if (src == SRC_NAND) 
	{
		/* Move image in NAND to memory */
		term_dat_out_crlf(rawnanddlns_msg);

	}
	else if (src == SRC_BLKDEV) 
	{
		/* Initialize FAT interface first */
		if (fat_init() == FALSE)
		{
			term_dat_out(blkdeverr_msg);
		}
		/* Try to open file */
		else if (fat_file_open(filename) == FALSE) 
		{
			term_dat_out_crlf(nofilmsg);
		}
		else 
		{
			fdata->num_bytes = 0;
			readloop = TRUE;
			while (readloop == TRUE) 
			{
				rb = fat_file_read(ptr8, 8);
				fdata->num_bytes += rb;
				ptr8 += 8;
				if (rb != 8) 
				{
					readloop = FALSE;
				}
			}
			fdata->contiguous = TRUE;
			loaded = TRUE;
		}

		fat_deinit();
	}

	return loaded;
}
Пример #23
0
/***********************************************************************
 *
 * Function: srec_parse
 *
 * Purpose: Load and parse an S-record file
 *
 * Processing:
 *     See function.
 *
 * Parameters:
 *     fdata    : Pointer to file data to fill
 *     src      : Data source
 *     filename : Filename (sd cards only)
 *
 * Outputs: None
 *
 * Returns: E if the file was parsed and loaded, otherwise FALSE
 *
 * Notes: None
 *
 **********************************************************************/
BOOL_32 srec_parse(FILE_DATA_T *ft,
                   SRC_LOAD_T src,
                   UNS_8 *filename) {
    UNS_8 line[384];
    int idx;
    BOOL_32 fop = FALSE, parsed = TRUE, done = FALSE;

    ft->loadaddr = 0xFFFFFFFF;
    ft->flt = FLT_RAW; /* Loaded as an S-record, saved as RAW */
    ft->num_bytes = 0;
    ft->startaddr = (PFV) 0xFFFFFFFF;
    ft->contiguous = TRUE;
    ft->loaded = FALSE;

	/* Attempt to open file if from SD source */
	if (src == SRC_BLKDEV) 
	{
		/* Init FAT filesystem and block device */
		if (fat_init() == FALSE)
		{
			term_dat_out(blkdeverr_msg);
			done = TRUE;
		}
		else
		{
			/* Try to open file */
			parsed = fat_file_open(filename);
			fop = TRUE;
			if (parsed == FALSE)
			{
				term_dat_out_crlf(nofilmsg);
				done = TRUE;
			}
		}
	}
	else if (src == SRC_NAND) 
	{
		/* Initialize NAND streamer */
		if (stream_flash_init() == FALSE) 
		{
			term_dat_out_crlf(noiif_msg);
			done = TRUE;
		}
	}
	else 
	{
		term_dat_out_crlf(rawdl_msg);
	}

    while ((done == FALSE) && (ft->loaded == FALSE)) 
    {
        /* Read line from device */
        parsed = readline(line, src);
        if (parsed == TRUE) 
        {
			/* Skip whitespace */
			idx = skip_whitespace(line, 0);

			parsed &= srec_parse_line(&line [idx], ft);
        }
		if (parsed == FALSE) 
		{
			done = TRUE;
		}
    }

	if (fop == TRUE)
	{
		fat_deinit();
	}

	return parsed;
}