Exemple #1
0
void
masterboot(void)
{
  struct elfhdr *elf;
  struct proghdr *ph, *eph;
  void (*entry)(void);
  uint8_t* pa;

  elf = (struct elfhdr*) 0x10000; // scratch space

  // Read first page off disk
  read_segment((uint8_t*) elf, PAGESIZE, 0);

  // Is this a valid ELF?
  if (elf->magic != ELF_MAGIC)
    return;

  // Load each program segment (ignores ph flags)
  ph = (struct proghdr *) ((uint8_t*) elf + elf->phoff);
  eph = ph + elf->phnum;
  for (; ph < eph; ph++) {
    pa = (uint8_t*) ph->paddr;
    read_segment(pa, ph->filesz, ph->off);
    if (ph->memsz > ph->filesz)
      stosb(pa + ph->filesz, 0, ph->memsz - ph->filesz);
  }

  // Call the entry point from the ELF header
  entry = (void(*)(void))(elf->entry);
  entry();
}
Exemple #2
0
void load(void)
{
	struct elfhdr *elf;
  	struct proghdr *ph, *eph;
  	void (*entry)(void);

	// kernel loaded into physical address 0x10000	64KB处
	elf = (struct elfhdr*)0x10000; 					// scratch space

	 // Read 1st page off disk from 1st sector, 0 sector for bootsector
  	read_segment((uint)elf, SECTOR_SIZE * 8, 0);

	// Is this an ELF executable?
  	if(elf->magic != ELF_MAGIC)
    		goto bad;

	// Load each program segment (ignores ph flags).
  	ph = (struct proghdr*)((uchar*)elf + elf->phoff);
  	eph = ph + elf->phnum;
	for(; ph < eph; ph++)
    		read_segment(ph->va & 0xFFFFFF, ph->memsz, ph->offset);

	// Call the entry point from the ELF header.
  	// Does not return!
  	entry = (void(*)(void))(elf->entry & 0xFFFFFF);
  	entry();

bad:
  	outw(0x8A00, 0x8A00);
  	outw(0x8A00, 0x8E00);
  	for(;;);
}
int update_header_segment(unsigned segment, byte * buffer)
{
    TRACE_FUN(5, "update_header_segment");
    int result = 0;
    int status;

    if (buffer == NULL) {
        TRACE(5, "no input buffer specified");
        buffer = deblock_buffer;
        result = read_segment(used_header_segment, buffer, &status, 0);
        if (bad_sector_map_changed) {
            store_bad_sector_map(buffer);
        }
        if (failed_sector_log_changed) {
            update_failed_sector_log(buffer);
        }
    }
    if (result >= 0 && GET4(buffer, 0) != 0xaa55aa55) {
        TRACE(1, "wrong header signature found, aborting");
        result = -EIO;
    }
    if (result >= 0) {
        result = _write_segment(segment, buffer, 0);
        if (result >= 0 && runner_status == idle) {
            /*  Force flush for single segment instead of relying on
             *  flush in read_segment for multiple segments.
             */
            result = start_writing(WRITE_SINGLE);
            if (result >= 0 && ftape_state == writing) {
                result = loop_until_writes_done();
                prevent_flush();
            }
        }
#ifdef VERIFY_HEADERS
        if (result >= 0) {	/* read back and verify */
            result = read_segment(segment, scratch_buffer, &status, 0);
            /*  Should retry if soft error during read !
             *  TO BE IMPLEMENTED
             */
            if (result >= 0) {
                if (memcmp(buffer, scratch_buffer, sizeof(buffer)) == 0) {
                    result = 0;	/* verified */
                    TRACE(5, "verified");
                } else {
                    result = -EIO;	/* verify failed */
                    TRACE(5, "verify failed");
                }
            }
        }
#endif
    }
    TRACE_EXIT;
    return result;
}
// sender uses the function
int send_file(int sock, struct sockaddr_in *recv_adr, char *filename, akh_disconn_response *disconn_response)
{
    uint32_t seg_size = disconn_response->segment_size;
    uint32_t req_num = disconn_response->segment_num;
    uint32_t *seg_list = disconn_response->segment_list;
    uint32_t seg_num;

    akh_pdu_header header;
    char buf[MAX_BUFFER_SIZE];
    packet pac;
    size_t buf_len, pac_len;

    int i;
    for(i = 0; i < req_num; i++) {
        seg_num = seg_list[i];
        header = createHeader(SS, seg_num);
        buf_len = read_segment(buf, seg_size, seg_num, filename);
        pac_len = createPacket(&pac, &header, buf, buf_len);
        sendto(sock, pac, pac_len, 0, (struct sockaddr *)recv_adr, sizeof(*recv_adr));

        deletePacket(pac);

        puts("< send file segment >");
        displayHeader(header);
    }
    return 0;
}
Exemple #5
0
void reloadPosition()
{
    //create handle to dfHack API
    static bool firstLoad = 1;

    if (timeToReloadConfig) {
        contentLoader->Load();
        timeToReloadConfig = false;
    }

    int segmentHeight = ssConfig.single_layer_view ? 2 : ssState.Size.z;
    //load segment
    if(ssConfig.threading_enable) {
        if(!ssConfig.threadmade) {
            ssConfig.readThread = al_create_thread(threadedSegment, NULL);
            ssConfig.threadmade = 1;
        }
    }

    if(ssConfig.threading_enable) {
        al_start_thread(ssConfig.readThread);
    } else {
        read_segment(NULL);
    }

    firstLoad = 0;
}
Exemple #6
0
char* construct_path(char* env_var, char* defaults_path, char* dir_path) {
	FTS* fts;
	FTSENT* ent;

	char* result = calloc(sizeof(char), 1);

	char* dirpathv[] = { defaults_path, dir_path, NULL };
	fts = fts_open(dirpathv, FTS_PHYSICAL | FTS_XDEV, NULL);
	if (!fts) {
		perror(dir_path);
		return NULL;
	}

	while ((ent = fts_read(fts)) != NULL) {
		// only interested in regular files, one level deep
		if (ent->fts_info != FTS_F) {
			if (ent->fts_level >= 1) fts_set(fts, ent, FTS_SKIP);
			continue;
		}

		FILE* f = fopen(ent->fts_accpath, "r");
		if (f == NULL) {
			perror(ent->fts_accpath);
			continue;
		}

		for (;;) {
			size_t len;
			char* line = fgetln(f, &len);
			if (line == NULL) break;
			char* segment = read_segment(line, len);
			
			append_path_segment(&result, segment);
		}

		fclose(f);
	}
	fts_close(fts);
	
	// merge in any existing custom PATH elemenets
	char* str = getenv(env_var);
	if (str) str = strdup(str);
	while (str) {
		char* sep = strchr(str, ':');
		if (sep) *sep = 0;
		
		append_path_segment(&result, str);
		if (sep) {
			str = sep + 1;
		} else {
			str = NULL;
		}
	}
	
	return result;
}
Exemple #7
0
static void * threadedSegment(ALLEGRO_THREAD *read_thread, void *arg)
{
    while(!al_get_thread_should_stop(read_thread)) {
        map_segment.lockRead();
        read_segment(arg);
        map_segment.unlockRead();
        al_rest(ssConfig.automatic_reload_time/1000.0);
    }
    return 0;
}
int ftape_update_header_segments(byte * buffer, int update)
{
    TRACE_FUN(5, "ftape_update_header_segments");
    int result = 0;
    int dummy;
    int header_changed = 1;

    if (ftape_state == writing) {
        result = loop_until_writes_done();
    }
    if (read_only) {
        result = 0;	/* exit and fake success */
        TRACE(4, "Tape set read-only: no update");
    } else if (result >= 0) {
        result = ftape_abort_operation();
        if (result >= 0) {
            if (buffer == NULL) {
                if (bad_sector_map_changed || failed_sector_log_changed) {
                    ftape_seek_to_bot();	/* prevents extra rewind */
                    buffer = deblock_buffer;
                    result = read_segment(used_header_segment, buffer, &dummy, 0);
                    if (result < 0) {
                        TRACE_EXIT;
                        return result;
                    }
                }
                header_changed = 0;
            }
            if (update) {
                if (bad_sector_map_changed) {
                    store_bad_sector_map(buffer);
                    header_changed = 1;
                }
                if (failed_sector_log_changed) {
                    update_failed_sector_log(buffer);
                    header_changed = 1;
                }
            }
            if (header_changed) {
                ftape_seek_to_bot();	/* prevents extra rewind */
                result = ftape_write_header_segments(buffer);
            }
        }
    }
    TRACE_EXIT;
    return result;
}
Exemple #9
0
int
load_macho( const char *image_name )
{
    load_command_t cmd;
    mach_header_t hd;
    int i, pos, fd;

    if( (fd=open(image_name, O_RDONLY)) < 0 )
        fatal_err("opening %s", image_name );

    if( !is_macho(fd) ) {
        close( fd );
        return 1;
    }

    if( read(fd, &hd, sizeof(hd)) != sizeof(hd) )
        fatal("failed to read Mach-O header");

    pos = sizeof(hd);
    for( i=0; i<hd.ncmds; i++, pos+=cmd.cmdsize ) {
        if( pread(fd, &cmd, sizeof(cmd), pos) != sizeof(cmd) )
            break;
        lseek( fd, pos, SEEK_SET );

        if( cmd.cmd == LC_SYMTAB )
            continue;

        /* printm("\nLoad-CMD [%d]: %ld\n", i, cmd.cmd ); */
        switch( cmd.cmd ) {
        case LC_SEGMENT:
            read_segment( fd );
            break;

        case LC_THREAD:
        case LC_UNIXTHREAD:
            read_thread( fd );
            break;
        }
    }
    close( fd );
    return 0;
}
Exemple #10
0
void reloadDisplayedSegment()
{
    //create handle to dfHack API
    static bool firstLoad = 1;

    if (timeToReloadConfig) {
        parms.thread_connect = 0;
        contentLoader->Load();
        timeToReloadConfig = false;
    }

    if (firstLoad || ssConfig.follow_DFscreen) {
        ssState.DisplayedSegment.x = parms.x;
        ssState.DisplayedSegment.y = parms.y;
        ssState.DisplayedSegment.z = parms.z;
    }

    int segmentHeight = ssConfig.single_layer_view ? 2 : ssState.SegmentSize.z;
    //load segment
    if(ssConfig.threading_enable) {
        if(!ssConfig.threadmade) {
            ssConfig.readThread = al_create_thread(threadedSegment, NULL);
            ssConfig.threadmade = 1;
        }
    }

    parms.x = ssState.DisplayedSegment.x;
    parms.y = ssState.DisplayedSegment.y;
    parms.z = ssState.DisplayedSegment.z;
    parms.sizex = ssState.SegmentSize.x;
    parms.sizey = ssState.SegmentSize.y;
    parms.sizez = segmentHeight;

    if(ssConfig.threading_enable) {
        al_start_thread(ssConfig.readThread);
    } else {
        read_segment(NULL);
    }

    firstLoad = 0;
}
int ftape_fix(void)
{
    TRACE_FUN(5, "ftape_fix");
    int result = 0;
    int dummy;
    int status;

    if (write_protected) {
        result = -EROFS;
    } else {
        /*  This will copy header segment 2 to header segment 1
         *  Spares us a tape format operation if header 2 is still good.
         */
        header_segment_1 = 0;
        header_segment_2 = 1;
        first_data_segment = 2;
        result = read_segment(header_segment_2, scratch_buffer, &dummy, 0);
        result = ftape_ready_wait(timeout.pause, &status);
        result = ftape_write_header_segments(scratch_buffer);
    }
    TRACE_EXIT;
    return result;
}
/*! Read segments from a tsv file into a sorted minimal linked list, and write
 *
 * @param in the input file
 * @param out the output file
 */
void
merge_segments (FILE * in, FILE * out)
{
  segment s;	     /* the current working segment */
  slist_ends ends = {NULL, NULL}; /* the limits of the segment list */
  segment_link /*@null@*/ /*@dependent@*/ *c = NULL; /* pointed to segment in list being consedered */
  segment_link /*@null@*/ /*@dependent@*/ *n = NULL; /* pointed to segment after one being consedered */
  int last_field;		/* the last field in the new segment */
  bool eof_found;               /* end of file marker encounetered */

  /* If we reach the end of the input right away, exist without doing anything */
  eof_found = !read_segment(in, &s);
  if (eof_found)
    {
      return;
    }
  
  link_segment(NULL, &ends, s);

  /* Work through the remaining input lines */
  while (read_segment(in, &s))
    {
      last_field = s.field + s.nfields - 1;

      /* Find the location in the list to put the new entry. */

      /* c points to the segment after which we will add the new segment */

      /* Start from the end, because this will be faster if the input 
       * already in order.
       */
      c = ends.last;
      while (c != NULL 
	     && s.run < c->segment.run)
	c = (segment_link *) c->previous;
      while (c != NULL 
	     && s.run == c->segment.run 
	     && s.rerun < c->segment.rerun)
	c = (segment_link *) c->previous;
      while (c != NULL
	     && s.run == c->segment.run 
	     && s.rerun == c->segment.rerun 
	     && s.camcol < c->segment.camcol)
	c = (segment_link *) c->previous;
      while (c != NULL 
	     && s.run == c->segment.run 
	     && s.rerun == c->segment.rerun 
	     && s.camcol == c->segment.camcol
	     && s.field < c->segment.field)
	c = (segment_link *) c->previous;

      /* check for potential merges */
      /* See if the s can be merged with the segment that preceeds it */
      if (c != NULL 
	  && s.run == c->segment.run && s.rerun == c->segment.rerun 
	  && s.camcol == c->segment.camcol)
	{
	  /* check whether the previous segment completely includes s */
	  if (c->segment.field + c->segment.nfields - 1 >= last_field)
	    {
	      continue;
	    }
	  /* check whether we can expand the previous segment to include s */
	  if (c->segment.field + c->segment.nfields >= s.field)
	    {
	      c->segment.nfields = last_field - c->segment.field + 1;
	      continue;
	    }
	}
      
      /* See if s can be merged by the segment that follows it */
      n = c == NULL ? ends.first : (segment_link *) c->next; 
      if (n != NULL 
	  && s.run == n->segment.run && s.rerun == n->segment.rerun 
	  && s.camcol == n->segment.camcol)
	{
	  /* check whether s overlaps the next one */
	  if (last_field >= n->segment.field - 1)
	    {
	      last_field = n->segment.field + n->segment.nfields - 1 > last_field
		? n->segment.field + n->segment.nfields - 1: last_field;
	      n->segment.field = s.field;
	      n->segment.nfields = last_field - n->segment.field + 1;
	      continue;
	    }
	}

      link_segment(c, &ends, s);

      /* print_segments(ends.first, stderr); */
      
    }

  print_segments(ends.first, out);
  fflush(out);

  free_segment_link (ends.first);
  
}