Exemple #1
0
uint32_t loader() {
	Elf32_Ehdr *elf;
	Elf32_Phdr *ph = NULL;

	uint8_t buf[4096];

#ifdef HAS_DEVICE
	ide_read(buf, ELF_OFFSET_IN_DISK, 4096);
#else
	ramdisk_read(buf, ELF_OFFSET_IN_DISK, 4096);
#endif

	elf = (void*)buf;

	/* fix the magic number with the correct one */
	const uint32_t elf_magic = 0x464c457f;
	uint32_t *p_magic = (void *)buf;
	nemu_assert(*p_magic == elf_magic);
	/* Load each program segment */
	int i;
	for(i=0;i<elf->e_phnum;i++ ) {
		/* Scan the program header table, load each segment into memory */
		ph=(void *)(elf->e_phoff + i * elf->e_phentsize + buf);
		if(ph->p_type == PT_LOAD) {
		//Log("success");
#ifdef IA32_PAGE
			/* Record the program break for future use. */
			extern uint32_t brk;
			uint32_t new_brk = ph->p_vaddr + ph->p_memsz - 1;
     		if(brk < new_brk) { brk = new_brk; }
			uint32_t pa=mm_malloc(ph->p_vaddr,ph->p_memsz);
#endif  
#ifndef HAS_DEVICE
			ramdisk_read((void*)pa, ELF_OFFSET_IN_DISK + ph->p_offset, ph->p_filesz); 
#else
			ide_read((void*)pa, ELF_OFFSET_IN_DISK + ph->p_offset, ph->p_filesz);
#endif
			memset((void*)(pa + ph->p_filesz), 0, ph->p_memsz - ph->p_filesz);
		}
	}

	volatile uint32_t entry = elf->e_entry;

#ifdef IA32_PAGE
	mm_malloc(KOFFSET - STACK_SIZE, STACK_SIZE);

#ifdef HAS_DEVICE
	create_video_mapping();
#endif

	write_cr3(get_ucr3());
#endif

	return entry;
}
Exemple #2
0
int
ramdisk_ioctl(rtems_disk_device *dd, uint32_t req, void *argp)
{
    switch (req)
    {
        case RTEMS_BLKIO_REQUEST:
        {
            rtems_blkdev_request *r = argp;
            struct ramdisk *rd = rtems_disk_get_driver_data(dd);

            switch (r->req)
            {
                case RTEMS_BLKDEV_REQ_READ:
                    return ramdisk_read(rd, r);

                case RTEMS_BLKDEV_REQ_WRITE:
                    return ramdisk_write(rd, r);

                default:
                    errno = EINVAL;
                    return -1;
            }
            break;
        }

        default:
            return rtems_blkdev_ioctl (dd, req, argp);
            break;
    }

    errno = EINVAL;
    return -1;
}
Exemple #3
0
uint32_t loader() {
	Elf32_Ehdr *elf;
	Elf32_Phdr *ph = NULL;

	uint8_t buf[4096];

#ifdef HAS_DEVICE
	ide_read(buf, ELF_OFFSET_IN_DISK, 4096);
#else
	ramdisk_read(buf, ELF_OFFSET_IN_DISK, 4096);
#endif

	elf = (void*)buf;

	/* TODO: fix the magic number with the correct one */
	const uint32_t elf_magic = 0x7f454c46;
	uint32_t *p_magic = (void *)buf;
	nemu_assert(*p_magic == elf_magic);

	/* Load each program segment */
	for(; true; ) {
		/* Scan the program header table, load each segment into memory */
		if(ph->p_type == PT_LOAD) {	//PT_LOAT=1, Loadable program segment 

			/* TODO: read the content of the segment from the ELF file 
			 * to the memory region [VirtAddr, VirtAddr + FileSiz)
			 */
			 
			 
			/* TODO: zero the memory region 
			 * [VirtAddr + FileSiz, VirtAddr + MemSiz)
			 */


#ifdef IA32_PAGE
			/* Record the program break for future use. */
			extern uint32_t brk;
			uint32_t new_brk = ph->p_vaddr + ph->p_memsz - 1;
			if(brk < new_brk) { brk = new_brk; }
#endif
		}
	}

	volatile uint32_t entry = elf->e_entry;

#ifdef IA32_PAGE
	mm_malloc(KOFFSET - STACK_SIZE, STACK_SIZE);

#ifdef HAS_DEVICE
	create_video_mapping();
#endif

	write_cr3(get_ucr3());
#endif

	return entry;
}
Exemple #4
0
int32_t fat16_mount(struct superblock_t *superblock) {
	int i;
	uint32_t offset = 0;
	uint32_t boot_offset;

	Partition partitionTable[4];
	/* Read the partition table */
	offset = PARTITION_TABLE_OFFSET;
	ramdisk_read(offset, sizeof(Partition)*4, partitionTable);
	
	/* Check for valid FAT16 partition type */
	for (int i = 0; i < 4; i++) {
		if (partitionTable[i].type == 4 ||
			partitionTable[i].type == 6 ||
			partitionTable[i].type == 14) {
			if (debug) printk("Found FAT16 partition!\n");
			break;
		}
	}

	/* Calculate boot sector offset */
	if (i == 4) {
		if (debug) printk("No FAT16 partition found!\n");
		return -1;
	} else {
		boot_offset = 512 * partitionTable[i].start_sector;
	}

	/* Read the MBR */
	offset = boot_offset;
	ramdisk_read(offset, sizeof(FAT16BootSector), &bootSector);

	/* Calculate start locations of FAT, root dir, and data */
	fat_start = bootSector.reserved_sectors * bootSector.sector_size;
	root_start = fat_start + bootSector.sectors_per_FAT * bootSector.num_FATs * bootSector.sector_size;
	data_start = root_start + bootSector.num_root_entries * sizeof(DirEntry);

	return 0;
}
Exemple #5
0
/* Just use the file entry offset as the inode.
	Note: only looking in root directory */
int32_t fat16_get_inode(const char *name) {
	int i;
	int32_t inode = 0;
	uint32_t offset = root_start;
	FAT16DirEntry entry;

	/* Scan through root dir looking for matching filename */
	for (i = 0; i < bootSector.num_root_entries; i++) {
		inode = offset;

		ramdisk_read(offset, sizeof(FAT16DirEntry), &entry);

		if (!strncmp(name, entry.filename, 8)) {
			return inode;
		}
	}

	return -1;
}
/* ramdisk_ioctl --
 *     IOCTL handler for RAM disk device.
 *
 * PARAMETERS:
 *      dev  - device number (major, minor number)
 *      req  - IOCTL request code
 *      argp - IOCTL argument
 *
 * RETURNS:
 *     IOCTL return value
 */
static int
ramdisk_ioctl(dev_t dev, uint32_t req, void *argp)
{
    switch (req)
    {
        case BLKIO_REQUEST:
        {
            rtems_device_minor_number minor;
            blkdev_request *r = argp;
            struct ramdisk *rd;

            minor = rtems_filesystem_dev_minor_t(dev);
            if ((minor >= nramdisks) || !ramdisk[minor].initialized)
            {
                errno = ENODEV;
                return -1;
            }

            rd = ramdisk + minor;

            switch (r->req)
            {
                case BLKDEV_REQ_READ:
                    return ramdisk_read(rd, r);

                case BLKDEV_REQ_WRITE:
                    return ramdisk_write(rd, r);

                default:
                    errno = EBADRQC;
                    return -1;
            }
            break;
        }

        default:
            errno = EBADRQC;
            return -1;
    }
}
Exemple #7
0
uint32_t loader() {
	Elf32_Ehdr *elf;
	Elf32_Phdr *ph = NULL;

	uint8_t buf[4096];

#ifdef HAS_DEVICE
	ide_read(buf, ELF_OFFSET_IN_DISK, 4096);
#else
	ramdisk_read(buf, ELF_OFFSET_IN_DISK, 4096);
#endif

	elf = (void*)buf;

	/* DONE: fix the magic number with the correct one */
	const uint32_t elf_magic = 0x464c457f;
	uint32_t *p_magic = (void *)buf;
	nemu_assert(*p_magic == elf_magic);

	/* Load each program segment */
	//panic("please implement me");
	ph = (Elf32_Phdr *)(buf + elf->e_phoff);
	uint16_t i = 0;
	for(i = 0; i < elf->e_phnum; ++i) {
		//if(elf->e_phnum == 3) {HIT_BAD_TRAP;}
		/* Scan the program header table, load each segment into memory */
		if(ph->p_type == PT_LOAD) {

			uint32_t hwaddr = mm_malloc(ph->p_vaddr, ph->p_memsz);

			/* DONE: read the content of the segment from the ELF file to the memory region [VirtAddr, VirtAddr + FileSiz) */
			ramdisk_read((uint8_t *)hwaddr, ph->p_offset, ph->p_filesz); 
			 
			/* DONE: zero the memory region [VirtAddr + FileSiz, VirtAddr + MemSiz)	 */
			memset((void *)hwaddr + ph->p_filesz, 0, ph->p_memsz - ph->p_filesz);


#ifdef IA32_PAGE
			/* Record the program break for future use. */
			extern uint32_t brk;
			uint32_t new_brk = ph->p_vaddr + ph->p_memsz - 1;
			if(brk < new_brk) { brk = new_brk; }
#endif
		}
		ph++;
	}

	volatile uint32_t entry = elf->e_entry;

#ifdef IA32_PAGE
	mm_malloc(KOFFSET - STACK_SIZE, STACK_SIZE);

#ifdef HAS_DEVICE
	create_video_mapping();
#endif

	write_cr3(get_ucr3());
#endif

	return entry;
}
Exemple #8
0
uint32_t loader() {
    Elf32_Ehdr *elf;
    Elf32_Phdr *ph = NULL;

    uint8_t buf[HEAD_SIZE];
    uint8_t buf_[_SIZE_];

#ifdef HAS_DEVICE
    ide_read(buf, ELF_OFFSET_IN_DISK, HEAD_SIZE);
#else
    ramdisk_read(buf, ELF_OFFSET_IN_DISK, HEAD_SIZE);
#endif
    elf = (void*)buf;

    /* TODO: fix the magic number with the correct one */
    const uint32_t elf_magic = 0x464c457f;
    uint32_t *p_magic = (void *)buf;
    nemu_assert(*p_magic == elf_magic);

    int cnt;
    /* Load each program segment */
    for(cnt = 0; cnt < elf->e_phnum; ++ cnt) {
        /* Scan the program header table, load each segment into memory */
        /*
        #ifdef HAS_DEVICE
        		ide_read(buf_, ELF_OFFSET_IN_DISK + elf->e_ehsize + cnt * elf->e_phentsize, elf->e_phentsize);
        #else
        		ramdisk_read(buf_, ELF_OFFSET_IN_DISK + elf->e_ehsize + cnt * elf->e_phentsize, elf->e_phentsize);
        #endif
        */
        ph = (void*)(buf + elf->e_ehsize + cnt * elf->e_phentsize);
//		ph = (void*)buf_;
        if(ph->p_type == PT_LOAD) {
            /* TODO: read the content of the segment from the ELF file
             * to the memory region [VirtAddr, VirtAddr + FileSiz)
             */
#ifdef HAS_DEVICE
            ide_read(buf_, ELF_OFFSET_IN_DISK + ph->p_offset, ph->p_filesz);
#else
            ramdisk_read(buf_, ELF_OFFSET_IN_DISK + ph->p_offset, ph->p_filesz);
#endif

            uint32_t hwaddr =
                mm_malloc(ph->p_vaddr, ph->p_memsz);

            memcpy((void *)hwaddr, (void *)(buf_), ph->p_filesz);
            /* TODO: zero the memory region
             * [VirtAddr + FileSiz, VirtAddr + MemSiz)
             */

//			Log("%x %x %x %x", ph->p_vaddr, ph->p_filesz, ph->p_memsz, ph->p_vaddr + ph->p_filesz);
            memset((void *)hwaddr + ph->p_filesz, 0, ph->p_memsz - ph->p_filesz);

#ifdef IA32_PAGE
            /* Record the program break for future use. */
            extern uint32_t brk;
            uint32_t new_brk = ph->p_vaddr + ph->p_memsz - 1;
            if(brk < new_brk) {
                brk = new_brk;
            }
#endif
        }
    }

    volatile uint32_t entry = elf->e_entry;

#ifdef IA32_PAGE
    mm_malloc(KOFFSET - STACK_SIZE, STACK_SIZE);

#ifdef HAS_DEVICE
    create_video_mapping();
#endif
    write_cr3(get_ucr3());
#endif

//	HIT_GOOD_TRAP;
    return entry;
}
Exemple #9
0
uint32_t loader() {
	Elf32_Ehdr *elf;
	Elf32_Phdr *ph = NULL;

	uint8_t buf[4096];

#ifdef HAS_DEVICE
	ide_read(buf, ELF_OFFSET_IN_DISK, 4096);
#else
	ramdisk_read(buf, ELF_OFFSET_IN_DISK, 4096);
#endif
	elf = (void*)buf;
	int i;
	/* TODO: fix the magic number with the correct one */
	const uint32_t elf_magic = 0x464c457f;
	uint32_t *p_magic = (void *)buf;
	nemu_assert(*p_magic == elf_magic);
	/* Load each program segment */
	//panic("please implement me");
	for(ph=(Elf32_Phdr*)(buf+elf->e_phoff),i=0;i<elf->e_phnum;i++ ) {
		/* Scan the program header table, load each segment into memory */
		if(ph->p_type == PT_LOAD ) {
			/* TODO: read the content of the segment from the ELF file 
			 * to the memory region [VirtAddr, VirtAddr + FileSiz)
			 */
			uint32_t va=(uint32_t)(ph->p_vaddr);
			uint8_t* my_vaddr=(uint8_t*)mm_malloc(va,ph->p_memsz);
			ide_read(my_vaddr,ph->p_offset,ph->p_filesz);
			//void* rstart=(void*)0;
			//memcpy(my_vaddr,rstart+ph->p_offset,ph->p_filesz); 	
			/* TODO: zero the memory region 
			 * [VirtAddr + FileSiz, VirtAddr + MemSiz)
			 */
			my_vaddr+=ph->p_filesz;
			int j;
			for(j=0;j<ph->p_memsz-ph->p_filesz;j++)
				*(my_vaddr+j)=0;


#ifdef IA32_PAGE
			/* Record the program break for future use. */
			extern uint32_t brk;
			uint32_t new_brk = ph->p_vaddr + ph->p_memsz - 1;
			if(brk < new_brk) { brk = new_brk; }
#endif
			ph++;
		}
	}

	uint32_t entry = elf->e_entry;
#ifdef IA32_PAGE
	mm_malloc(KOFFSET - STACK_SIZE, STACK_SIZE);

#ifdef HAS_DEVICE
	create_video_mapping();
#endif

	write_cr3(get_ucr3());
#endif
	return entry;
}