Пример #1
0
static autostart_prg_t * load_prg(const char *file_name, fileio_info_t *finfo)
{
	DWORD ptr;
	DWORD end;
	BYTE lo,hi;
	int  i;
	autostart_prg_t *prg;

	prg = lib_malloc(sizeof(autostart_prg_t));
	if (prg == NULL) {
		return NULL;
	}

	/* get data size of file */
	prg->size = fileio_get_bytes_left(finfo);
	prg->data = NULL;

	/* read start address */
	if ((fileio_read(finfo, &lo, 1) != 1) || (fileio_read(finfo, &hi, 1) != 1))
	{
		//log_error(log, "Cannot read start address from '%s'", file_name);
		return NULL;
	}
	prg->start_addr = (WORD)hi << 8 | (WORD)lo;
	prg->size -= 2; /* skip load addr */

	/* check range */
	end = prg->start_addr + prg->size - 1;
	if (end > 0xffff) {
		//log_error(log, "Invalid size of '%s': %d", file_name, prg->size);
		return NULL;
	}

	/* load to memory */
	prg->data = lib_malloc(prg->size);
	if (prg->data == NULL) {
		//log_error(log, "No memory for '%s'", file_name);
		return NULL;        
	}

	/* copy data to memory */
	ptr = prg->start_addr;
	i = 0;
	while (ptr <= end) {
		if (fileio_read(finfo, &(prg->data[i]), 1) != 1) {
			//log_error(log, "Error loading data from '%s'", file_name);
			lib_free(prg->data);
			return NULL;
		}
		ptr++;
		i++;
	}

	return prg;
}
Пример #2
0
/*
 * logwr_fetch_header_page -
 *
 * return:
 *   log_pgptr(out):
 * Note:
 */
static int
logwr_fetch_header_page (LOG_PAGE * log_pgptr)
{
  PAGEID pageid;
  PAGEID phy_pageid;

  assert (log_pgptr != NULL);

  /*
   * Page is contained in the active log.
   * Find the corresponding physical page and read the page form disk.
   */
  pageid = LOGPB_HEADER_PAGE_ID;
  phy_pageid = logwr_to_physical_pageid (pageid);

  if (fileio_read (NULL, logwr_Gl.append_vdes, log_pgptr, phy_pageid,
		   LOG_PAGESIZE) == NULL)
    {
      er_set (ER_FATAL_ERROR_SEVERITY, ARG_FILE_LINE,
	      ER_LOG_READ, 3, pageid, phy_pageid, logwr_Gl.active_name);
      return ER_LOG_READ;
    }
  else
    {
      if (log_pgptr->hdr.logical_pageid != pageid)
	{
	  er_set (ER_FATAL_ERROR_SEVERITY, ARG_FILE_LINE,
		  ER_LOG_PAGE_CORRUPTED, 1, pageid);
	  return ER_LOG_PAGE_CORRUPTED;
	}
    }

  return NO_ERROR;
}
Пример #3
0
/*
 * log_read_op -- perform read operation
 */
static int
log_read_op(struct benchmark *bench, struct operation_info *info)
{
	struct log_bench *lb = (struct log_bench *)pmembench_get_priv(bench);
	assert(lb);

	struct log_worker_info *worker_info =
		(struct log_worker_info *)info->worker->priv;

	assert(worker_info);

	worker_info->buf_ptr = 0;

	size_t chunk_size = lb->args->rand
		? worker_info->rand_sizes[info->index]
		: lb->args->el_size;

	if (!lb->args->fileio) {
		pmemlog_walk(lb->plp, chunk_size, log_process_data,
			     worker_info);
		return 0;
	}

	int ret;
	while ((ret = fileio_read(lb->fd, chunk_size, worker_info)) == 1)
		;

	return ret;
}
Пример #4
0
/**
 * @returns If no error occurred, returns number of bytes consumed;
 * otherwise, returns a negative error code.)
 */
int nand_fileio_read(struct nand_device *nand, struct nand_fileio_state *s)
{
	size_t total_read = 0;
	size_t one_read;

	if (NULL != s->page) {
		fileio_read(&s->fileio, s->page_size, s->page, &one_read);
		if (one_read < s->page_size)
			memset(s->page + one_read, 0xff, s->page_size - one_read);
		total_read += one_read;
	}

	if (s->oob_format & NAND_OOB_SW_ECC) {
		uint8_t ecc[3];
		memset(s->oob, 0xff, s->oob_size);
		for (uint32_t i = 0, j = 0; i < s->page_size; i += 256) {
			nand_calculate_ecc(nand, s->page + i, ecc);
			s->oob[s->eccpos[j++]] = ecc[0];
			s->oob[s->eccpos[j++]] = ecc[1];
			s->oob[s->eccpos[j++]] = ecc[2];
		}
	} else if (s->oob_format & NAND_OOB_SW_ECC_KW)   {
		/*
		 * In this case eccpos is not used as
		 * the ECC data is always stored contigously
		 * at the end of the OOB area.  It consists
		 * of 10 bytes per 512-byte data block.
		 */
		uint8_t *ecc = s->oob + s->oob_size - s->page_size / 512 * 10;
		memset(s->oob, 0xff, s->oob_size);
		for (uint32_t i = 0; i < s->page_size; i += 512) {
			nand_calculate_ecc_kw(nand, s->page + i, ecc);
			ecc += 10;
		}
	} else if (NULL != s->oob)   {
		fileio_read(&s->fileio, s->oob_size, s->oob, &one_read);
		if (one_read < s->oob_size)
			memset(s->oob + one_read, 0xff, s->oob_size - one_read);
		total_read += one_read;
	}
	return total_read;
}
Пример #5
0
int advanced_elf_image_read_section(struct advanced_elf_image *elf, int section, void *buf, size_t buf_size, size_t *done)
{
    if (!elf || section < 0 || section >= elf->num_sections || !elf->sections)
        return ERROR_COMMAND_ARGUMENT_INVALID;
    
    int retval = fileio_seek(elf->fileio, elf->sections[section].sh_offset);
    if (retval != ERROR_OK)
        return retval;
    
    retval = fileio_read(elf->fileio, buf_size, buf, done);
    return retval;
}
Пример #6
0
/* Flash load body routine replacement.  */
int flash_trap_load_body(void)
{
    DWORD addr;
    BYTE laddr, maddr, haddr;

    /* bail out if true fs, not emulated */
    if (flash_trap_trueflashfs) {
        return 0;
    }

    /* start address */
    addr = mem_read(0xFB) | (mem_read(0xFC) << 8) | (mem_read(0xFD) << 16);

    /* load */
    if (fi) {
        BYTE b;
        while (fileio_read(fi, &b, 1)) {
            mem_ram[addr & 0x1FFFFF] = b;
            addr++;
        }
        fileio_close(fi);
        fi = NULL;
    }

    /* set exit values for success and return */
    laddr = (BYTE)(addr & 0xff);
    maddr = (BYTE)((addr >> 8) & 0xff);
    haddr = (BYTE)((addr >> 16) & 0xff);
    mem_store((WORD)0xFB, laddr);
    mem_store((WORD)0xFC, maddr);
    mem_store((WORD)0xFD, haddr);
    maincpu_set_x(laddr);
    maincpu_set_y(maddr);
    mem_store((WORD)0xAE, laddr);
    mem_store((WORD)0xAF, maddr);

    return 1;
}
Пример #7
0
/* Flash seek next routine replacement.
   Create valid directory entry at $0100
 */
int flash_trap_seek_next(void)
{
    unsigned int i;
    BYTE direntry[0x20];
    DWORD entry;

    /* bail out if true fs, not emulated */
    if (flash_trap_trueflashfs) {
        return 0;
    }

    /* if we are reading the very first entry in the flash, do
       initialization stuff. */
    entry = mem_read(0xF8) | (mem_read(0xF9) << 8) | (mem_read(0xFA) << 16);
    if (entry == 0x010000) {
        read_name_from_mem();

        if (name_len == 0) {
            /* the missing filename detection of the original kernal
               requires at least one valid entry to work. */
            name_len = 5;
            memcpy(name, "DUMMY", 5);
            seek_state = ST_ENTRY;
        } else {
            char *path = flash_trap_fsflashdir;
            if (!strlen(path)) {
                path = NULL;
            }

            /* open file */
            if (fi) {
                fileio_close(fi);
            }
            fi = fileio_open(name, path, FILEIO_FORMAT_RAW, FILEIO_COMMAND_READ, FILEIO_TYPE_ANY);
            if (fi) {
                BYTE addr[2];
                fileio_read(fi, addr, 2);
                load_addr = addr[0] | (addr[1] << 8);
                seek_state = ST_ENTRY;
            } else {
                seek_state = ST_END;
            }
        }
    }

    switch (seek_state) {
        case ST_ENTRY:
            memset(direntry, 0x00, sizeof(direntry));
            /* copy the actual searched name to force a match */
            memcpy(direntry, name, name_len);

            /* flash_address */
            direntry[0x18] = 0x11;
            direntry[0x19] = 0x10;
            direntry[0x1A] = 0x02;

            /* load_address */
            direntry[0x1B] = (BYTE)(load_addr & 0xff);
            direntry[0x1C] = (BYTE)((load_addr >> 8) & 0xff);
            direntry[0x1D] = (BYTE)((load_addr >> 16) & 0xff);

            /* sys_address (non-standard) */
            direntry[0x1E] = 0x00;
            direntry[0x1F] = 0x00;

            seek_state = ST_END;
            break;

        case ST_END:
            memset(direntry, 0x00, sizeof(direntry));
            seek_state = ST_EMPTY;
            break;

        default:
        case ST_EMPTY:
            memset(direntry, 0xFF, sizeof(direntry));
            break;
    }


    /* write out directory entry to the buffer at $0100-$011F */
    for (i = 0; i < sizeof(direntry); i++) {
        mem_store((WORD)(0x0100 + i), direntry[i]);
    }

    return 1;
}
Пример #8
0
static int command_read(bufinfo_t *bufinfo, BYTE *data)
{
    if (bufinfo->tape->name) {
        if (bufinfo->buflen > 0) {
            *data = *bufinfo->bufp++;
            bufinfo->buflen--;
        } else {
            /* If we are already at an EOF state, check next read, next stream
               may be available */
            if (bufinfo->iseof) {
                *data = 0xc7;
                bufinfo->iseof = !tape_read(bufinfo->tape,
                                            &(bufinfo->buffered), 1);
                bufinfo->isbuffered = 1;
                if (bufinfo->iseof)
                    return SERIAL_EOF;
            }
            /* If this is our first read, read in first byte */
            if (!bufinfo->isbuffered) {
                bufinfo->iseof = !tape_read(bufinfo->tape,
                                            &(bufinfo->buffered), 1);
                /* XXX We shouldn't get an EOF at this point, or can we? */
            }
            /* Place it in the output field */
            *data = bufinfo->buffered;
            /* Read the next buffer; if nothing read, set EOF signal */
            bufinfo->iseof = !tape_read(bufinfo->tape, &(bufinfo->buffered), 1);
            /* Indicate we have something in the buffer for the next read */
            bufinfo->isbuffered = 1;
            /* If the EOF was signaled, return a CBM EOF */
            if (bufinfo->iseof)
                return SERIAL_EOF;
            /* If not, return OK */
            return SERIAL_OK;

#if 0
            if (tape_read(bufinfo->tape, data, 1) != 1) {
                *data = 0xc7;
                return SERIAL_EOF;
            }
#endif
        }
        return SERIAL_OK;
    } else {
        if (bufinfo->fileio_info) {
            /* If we are already at an EOF state, check next read, next stream
               may be available */
            if (bufinfo->iseof) {
                *data = 0xc7;
                bufinfo->iseof = !tape_read(bufinfo->tape,
                                 &(bufinfo->buffered), 1);
                bufinfo->isbuffered = 1;
                if (bufinfo->iseof)
                    return SERIAL_EOF;
            }
            /* If this is our first read, read in first byte */
            if (!bufinfo->isbuffered) {
                bufinfo->iseof = !fileio_read(bufinfo->fileio_info,
                                 &(bufinfo->buffered), 1);
                /* We shouldn't get an EOF at this point */
                /* Check for errors */
                if (fileio_ferror(bufinfo->fileio_info))
                    return SERIAL_ERROR;
            }
            /* Place it in the output field */
            *data = bufinfo->buffered;
            /* Read the next buffer; if nothing read, set EOF signal */
            bufinfo->iseof = !fileio_read(bufinfo->fileio_info,
                             &(bufinfo->buffered), 1);
            /* Check for errors */
            if (fileio_ferror(bufinfo->fileio_info))
                return SERIAL_ERROR;
            /* Indicate we have something in the buffer for the next read */
            bufinfo->isbuffered = 1;
            /* If the EOF was signaled, return a CBM EOF */
            if (bufinfo->iseof)
                return SERIAL_EOF;
            /* If not, return OK */
            return SERIAL_OK;

#if 0
            unsigned int len;

            len = fileio_read(bufinfo->info, data, 1);

            if (fileio_ferror(bufinfo->info))
                return SERIAL_ERROR;
 
            if (len == 0) {
                *data = 0xc7;
                return SERIAL_EOF;
            }
            return SERIAL_OK;
#endif
        }
    }

    return FLOPPY_ERROR;
}
Пример #9
0
int advanced_elf_image_open(struct advanced_elf_image *elf, const char *URL)
{
    memset(elf, 0, sizeof(struct advanced_elf_image));
    int retval = fileio_open(&elf->fileio, URL, FILEIO_READ, FILEIO_BINARY);
    size_t done;
    if (retval != ERROR_OK)
        return retval;
    
    retval = fileio_read(elf->fileio, sizeof(elf->header), &elf->header, &done);
    if (retval != ERROR_OK)
        return retval;
    
    if (strncmp((char *)elf->header.e_ident, ELFMAG, SELFMAG) != 0) {
        LOG_ERROR("invalid ELF file, bad magic number");
        return ERROR_IMAGE_FORMAT_ERROR;
    }
    if (elf->header.e_ident[EI_CLASS] != ELFCLASS32) {
        LOG_ERROR("invalid ELF file, only 32bits files are supported");
        return ERROR_IMAGE_FORMAT_ERROR;
    }
    
    elf->num_sections = elf->header.e_shnum;
    elf->sections = (Elf32_Shdr *)calloc(elf->header.e_shnum, sizeof(Elf32_Shdr));
    retval = fileio_seek(elf->fileio, elf->header.e_shoff);
    if (retval != ERROR_OK)
        return retval;
    
    retval = fileio_read(elf->fileio, sizeof(Elf32_Shdr) * elf->header.e_shnum, elf->sections, &done);
    if (retval != ERROR_OK)
        return retval;
    
    for (int i = 0; i < elf->num_sections; i++)
        if (elf->sections[i].sh_type == 2 /*SHT_SYMTAB*/)
        {
            if (elf->sections[i].sh_entsize != sizeof(Elf32_Sym))
            {
                LOG_ERROR("Unexpected symtab entry size in %s: %d.", URL, elf->sections[i].sh_entsize);
                return ERROR_IMAGE_FORMAT_ERROR;       
            }
    
            elf->num_symbols = elf->sections[i].sh_size / elf->sections[i].sh_entsize;
            elf->symbols = calloc(elf->num_symbols, elf->sections[i].sh_entsize);
            retval = fileio_seek(elf->fileio, elf->sections[i].sh_offset);
            if (retval != ERROR_OK)
                return retval;
            
            retval = fileio_read(elf->fileio, sizeof(Elf32_Sym) * elf->num_symbols, elf->symbols, &done);
            if (retval != ERROR_OK)
                return retval;
            if (done != (sizeof(Elf32_Sym) * elf->num_symbols))
                return ERROR_IMAGE_FORMAT_ERROR;
            
            int str = elf->sections[i].sh_link;
            if (str < 0 || str >= elf->header.e_shnum || elf->sections[str].sh_type != 3 /*SHT_STRTAB*/)
            {
                LOG_ERROR("Invalid strtab link from symtab section");
                return ERROR_IMAGE_FORMAT_ERROR;       
            }
            
            elf->strtab_size = elf->sections[str].sh_size;
            elf->strtab = malloc(elf->sections[str].sh_size);
            
            retval = fileio_seek(elf->fileio, elf->sections[str].sh_offset);
            if (retval != ERROR_OK)
                return retval;
            
            retval = fileio_read(elf->fileio, elf->strtab_size, elf->strtab, &done);
            if (retval != ERROR_OK)
                return retval;
            if (done != elf->strtab_size)
                return ERROR_IMAGE_FORMAT_ERROR;

            
            break;
        }
    
    if (!elf->symbols || !elf->strtab)
    {
        LOG_ERROR("The FLASH plugin file does not contain a symbol table and cannot be loaded.");
        return ERROR_IMAGE_FORMAT_ERROR;       
    }
    
    //TODO: actually read sections
    return retval;
}