static int do_bootz_linux_fdt(int fd, struct image_data *data) { struct fdt_header __header, *header; struct resource *r = data->os_res; struct resource *of_res = data->os_res; void *oftree; int ret; u32 end; header = &__header; ret = read(fd, header, sizeof(*header)); if (ret < sizeof(*header)) return ret; if (file_detect_type(header) != filetype_oftree) return -ENXIO; end = be32_to_cpu(header->totalsize); if (IS_BUILTIN(CONFIG_OFTREE)) { oftree = malloc(end + 0x8000); if (!oftree) { perror("zImage: oftree malloc"); return -ENOMEM; } } else { of_res = request_sdram_region("oftree", r->start + resource_size(r), end); if (!of_res) { perror("zImage: oftree request_sdram_region"); return -ENOMEM; } oftree = (void*)of_res->start; } memcpy(oftree, header, sizeof(*header)); end -= sizeof(*header); ret = read_full(fd, oftree + sizeof(*header), end); if (ret < 0) return ret; if (ret < end) { printf("premature end of image\n"); return -EIO; } if (IS_BUILTIN(CONFIG_OFTREE)) { fdt_open_into(oftree, oftree, end + 0x8000); ret = of_fix_tree(oftree); if (ret) return ret; data->oftree = oftree; } pr_info("zImage: concatenated oftree detected\n"); return 0; }
/* urandom_rand_pseudo_bytes puts |num| random bytes into |out|. It returns * one on success and zero otherwise. */ int RAND_bytes(uint8_t *out, size_t requested) { int fd; struct rand_buffer *buf; size_t todo; pid_t pid, ppid; if (requested == 0) { return 1; } CRYPTO_w_lock(CRYPTO_LOCK_RAND); fd = urandom_get_fd_locked(); if (fd < 0) { CRYPTO_w_unlock(CRYPTO_LOCK_RAND); abort(); return 0; } /* If buffering is not enabled, or if the request is large, then the * result comes directly from urandom. */ if (!urandom_buffering || requested > BUF_SIZE / 2) { CRYPTO_w_unlock(CRYPTO_LOCK_RAND); if (!read_full(fd, out, requested)) { abort(); return 0; } return 1; } pid = getpid(); ppid = getppid(); for (;;) { buf = list_head; if (buf && buf->pid == pid && buf->ppid == ppid && rand_bytes_per_buf - buf->used >= requested) { memcpy(out, &buf->rand[buf->used], requested); buf->used += requested; CRYPTO_w_unlock(CRYPTO_LOCK_RAND); return 1; } /* If we don't immediately have enough entropy with the correct * PID, remove the buffer from the list in order to gain * exclusive access and unlock. */ if (buf) { list_head = buf->next; } CRYPTO_w_unlock(CRYPTO_LOCK_RAND); if (!buf) { buf = (struct rand_buffer *)OPENSSL_malloc(BUF_SIZE); if (!buf) { abort(); return 0; } /* The buffer doesn't contain any random bytes yet * so we mark it as fully used so that it will be * filled below. */ buf->used = rand_bytes_per_buf; buf->next = NULL; buf->pid = pid; buf->ppid = ppid; } if (buf->pid == pid && buf->ppid == ppid) { break; } /* We have forked and so cannot use these bytes as they * may have been used in another process. */ OPENSSL_free(buf); CRYPTO_w_lock(CRYPTO_LOCK_RAND); } while (requested > 0) { todo = rand_bytes_per_buf - buf->used; if (todo > requested) { todo = requested; } memcpy(out, &buf->rand[buf->used], todo); requested -= todo; out += todo; buf->used += todo; if (buf->used < rand_bytes_per_buf) { break; } if (!read_full(fd, buf->rand, rand_bytes_per_buf)) { OPENSSL_free(buf); abort(); return 0; } buf->used = 0; } CRYPTO_w_lock(CRYPTO_LOCK_RAND); assert(list_head != buf); buf->next = list_head; list_head = buf; CRYPTO_w_unlock(CRYPTO_LOCK_RAND); return 1; }
static int do_bootz_linux_fdt(int fd, struct image_data *data) { struct fdt_header __header, *header; void *oftree; int ret; u32 end; if (data->oftree) return -ENXIO; header = &__header; ret = read(fd, header, sizeof(*header)); if (ret < sizeof(*header)) return ret; if (file_detect_type(header, sizeof(*header)) != filetype_oftree) return -ENXIO; end = be32_to_cpu(header->totalsize); oftree = malloc(end + 0x8000); if (!oftree) { perror("zImage: oftree malloc"); return -ENOMEM; } memcpy(oftree, header, sizeof(*header)); end -= sizeof(*header); ret = read_full(fd, oftree + sizeof(*header), end); if (ret < 0) goto err_free; if (ret < end) { printf("premature end of image\n"); ret = -EIO; goto err_free; } if (IS_BUILTIN(CONFIG_OFTREE)) { struct device_node *root; root = of_unflatten_dtb(oftree); if (IS_ERR(root)) { pr_err("unable to unflatten devicetree\n"); goto err_free; } data->oftree = of_get_fixed_tree(root); if (!data->oftree) { pr_err("Unable to get fixed tree\n"); ret = -EINVAL; goto err_free; } free(oftree); } else { data->oftree = oftree; } pr_info("zImage: concatenated oftree detected\n"); return 0; err_free: free(oftree); return ret; }
/* * read ddp_hdr_start in ddphdr_blk. * pass ddp_hdr_start to ddp layer to determine header size * read rest of ddphdr_blk * get buffer from ddp layer * generate recv vector * read into recv vector * do crc check. */ static int mpa_rd_mrkr_fpdu(iwsk_t *iwsk, uint32_t *bidx, uint32_t *midx) { mpa_recv_cntr++; stream_pos_t sp = iwsk->mpask.recv_sp; marker_pos_t mp = iwsk->mpask.recv_mp - sp; marker_pos_t mk = mp; uint32_t hdrsz = 0, cp = 0, pl = 0, ln = 0, hp = 0, lp = 0, crc = 0; uint32_t st_bidx = *bidx; /* starting block index */ uint8_t *cur = NULL; uint8_t pad = 0; buf_t b; int ret; memset(ddphdr_blk, 0, DDP_MAX_HDR_SZ); if (mp == 0) { mpa_read_mrkr(iwsk, bidx, midx, &cp, &mp); } read_full(iwsk->sk, ddphdr_blk, sizeof(ddp_hdr_start_t)); hdrsz = ddp_get_hdr_sz(ddphdr_blk); if (mp >= hdrsz) { read_full(iwsk->sk, (uint8_t *)ddphdr_blk + sizeof(ddp_hdr_start_t), hdrsz - sizeof(ddp_hdr_start_t)); /* we need hdr for crc calculation */ mpa_fill_blk(blks, bidx, ddphdr_blk, hdrsz, &cp); } else { read_full(iwsk->sk, (uint8_t *)ddphdr_blk + sizeof(ddp_hdr_start_t), mp - sizeof(ddp_hdr_start_t)); /* we need hdr for crc calculation */ mpa_fill_blk(blks, bidx, ddphdr_blk, mp, &cp); mpa_read_mrkr(iwsk, bidx, midx, &cp, &mp); read_full(iwsk->sk, (uint8_t *)ddphdr_blk + (mp - MARKER_PERIOD), hdrsz - (mp - MARKER_PERIOD)); /* we need hdr for crc calculation */ mpa_fill_blk(blks, bidx, (uint8_t *)ddphdr_blk + (mp - MARKER_PERIOD), hdrsz - (mp - MARKER_PERIOD), &cp); } ret = ddp_get_sink(iwsk, ddphdr_blk, &b); if (ret < 0) return ret; cur = b.buf; pl = b.len; st_bidx = *bidx; /* readv from st_bidx, ignore already read parts */ hp = cp; /* hp: position of last byte of header in fpdu */ pad = WORD_SZ*((b.len+WORD_SZ-1)/WORD_SZ) - b.len; /* 4 - b.len%4 */ debug(2, "pl %d", pl); while (pl > 0) { ln = mp - cp; if (ln > pl) ln = pl; mpa_fill_blk(blks, bidx, cur, ln, &cp); cur += ln; pl -= ln; if (pl == 0 && pad) { pad_blk = 0; mpa_fill_blk(blks, bidx, &pad_blk, pad, &cp); } if (cp == mp) { mpa_fill_blk(blks, bidx, &mrkr_blk[*midx], MARKER_SZ, &cp); mp += MARKER_PERIOD; (*midx)++; } } iw_assert(cur == ((uint8_t *)b.buf + b.len), "cur(%p) != (b.buf + b.len)(%p)", cur, ((uint8_t *)b.buf + b.len)); iwsk->mpask.recv_sp += cp; iwsk->mpask.recv_mp += (*midx)*MARKER_PERIOD; if (iwsk->mpask.use_crc) mpa_fill_blk(blks, bidx, &crc_blk, CRC_SZ, &cp); readv_full(iwsk->sk, &blks[st_bidx], *bidx - st_bidx, cp - hp); for (lp = 0; lp < *midx; lp++) { if(mrkr_blk[lp].fpduptr != mk + lp*MARKER_PERIOD) { printerr("fpduptr (%d), expected (%d)", mrkr_blk[lp].fpduptr, mk + lp*MARKER_PERIOD); return -EBADMSG; } } if(iwsk->mpask.recv_mp - sp != mk + (*midx * MARKER_PERIOD)) { printerr("new mp(%d) != expected new mp(%d)", iwsk->mpask.recv_mp - sp, mk + (*midx * MARKER_PERIOD)); return -EBADMSG; } if (iwsk->mpask.use_crc) { crc = crc32c_vec(blks, *bidx - 1); crc_blk = ntohl(crc_blk); debug(4, "%s: crc %x crc_blk %x bidx %u", __func__, crc, crc_blk, *bidx - 1); if (crc != crc_blk) printerr("crc check failed. exp %x got %x", crc, crc_blk); /* TODO: Surface this error */ return -EBADMSG; } return 0; }
static int uimage_fill(void *buf, unsigned int len) { return read_full(uimage_fd, buf, len); }
void *read_chk_file(void){ int bytes_received = 0; int file_fd; ram_addr_t adr,index=0,flags; int j=0,n,count=0,count_allocate =0; gettimeofday(&startfileret, NULL); if((file_fd = open(chkpt_filename, O_RDONLY))<0){ printf("mem server: cannot open chk file for read error no = %d\n", errno); fflush(stdout); exit(1); } if(values == NULL){ values = (mempage**) calloc (total_pages_param,sizeof(mempage *)); } // read contents bytes_received =read_full(file_fd,&adr,sizeof(adr)); if (bytes_received == -1) { fprintf(stderr,"recv addr failed :%s\n",strerror(errno)); exit(1); } flags = adr &~TARGET_PAGE_MASK ; adr &= TARGET_PAGE_MASK ; while(flags != MY_EOF){ count++; index = adr >> TARGET_PAGE_BITS ; index = index/num_serv ; // 2.Recv data-page if(*(values+index)==NULL){ values[index]=(mempage*)malloc(sizeof(mempage)); count_allocate++; } bytes_received =read_full(file_fd,values[index],sizeof(mempage)); if (bytes_received == -1) { fprintf(stderr,"recv values failed :%s\n",strerror(errno)); exit(1); } bytes_received =read_full(file_fd,&adr,sizeof(adr)); if (bytes_received == -1) { fprintf(stderr,"recv addr failed :%s\n",strerror(errno)); exit(1); } flags = adr &~TARGET_PAGE_MASK ; adr &= TARGET_PAGE_MASK ; }//end while MY_EOF close(file_fd); printf("read file count recv = %d ,count_allocate = %d \n",count,count_allocate); fflush(stdout); gettimeofday(&endfileret, NULL); fileret_elasp = ((endfileret.tv_sec * 1000000 + endfileret.tv_usec)-(startfileret.tv_sec * 1000000 + startfileret.tv_usec)); printf("Elasp time retrieving pages from file = %"PRId64" ms\n", (uint64_t)(fileret_elasp/1000)); pthread_mutex_lock(&mutex); read_file_done = 1; pthread_cond_signal(&read_file_cond); pthread_mutex_unlock(&mutex); pthread_exit(NULL); }
/** * compare_file - Compare two files * @f1: The first file * @f2: The second file * * Return: 0 if both files are identical, 1 if they differ, * a negative error code if some error occured */ int compare_file(const char *f1, const char *f2) { int fd1, fd2, ret; struct stat s1, s2; void *buf1, *buf2; loff_t left; fd1 = open(f1, O_RDONLY); if (fd1 < 0) return -errno; fd2 = open(f2, O_RDONLY); if (fd2 < 0) { ret = -errno; goto err_out1; } ret = fstat(fd1, &s1); if (ret) goto err_out2; ret = fstat(fd2, &s2); if (ret) goto err_out2; if (s1.st_size != s2.st_size) return 1; buf1 = xmalloc(RW_BUF_SIZE); buf2 = xmalloc(RW_BUF_SIZE); left = s1.st_size; while (left) { loff_t now = min(left, (loff_t)RW_BUF_SIZE); ret = read_full(fd1, buf1, now); if (ret < 0) goto err_out3; ret = read_full(fd2, buf2, now); if (ret < 0) goto err_out3; if (memcmp(buf1, buf2, now)) { ret = 1; goto err_out3; } left -= now; } ret = 0; err_out3: free(buf1); free(buf2); err_out2: close(fd2); err_out1: close(fd1); return ret; }
static int do_bootz_linux(struct image_data *data) { int fd, ret, swap = 0; struct zimage_header __header, *header; void *zimage; u32 end; unsigned long load_address = data->os_address; if (load_address == UIMAGE_INVALID_ADDRESS) { struct memory_bank *bank = list_first_entry(&memory_banks, struct memory_bank, list); data->os_address = bank->start + SZ_8M; load_address = data->os_address; if (bootm_verbose(data)) printf("no os load address, defaulting to 0x%08lx\n", load_address); } fd = open(data->os_file, O_RDONLY); if (fd < 0) { perror("open"); return 1; } header = &__header; ret = read(fd, header, sizeof(*header)); if (ret < sizeof(*header)) { printf("could not read %s\n", data->os_file); goto err_out; } switch (header->magic) { case swab32(ZIMAGE_MAGIC): swap = 1; /* fall through */ case ZIMAGE_MAGIC: break; default: printf("invalid magic 0x%08x\n", header->magic); ret = -EINVAL; goto err_out; } end = header->end; if (swap) end = swab32(end); data->os_res = request_sdram_region("zimage", load_address, end); if (!data->os_res) { pr_err("bootm/zImage: failed to request memory at 0x%lx to 0x%lx (%d).\n", load_address, load_address + end, end); ret = -ENOMEM; goto err_out; } zimage = (void *)data->os_res->start; memcpy(zimage, header, sizeof(*header)); ret = read_full(fd, zimage + sizeof(*header), end - sizeof(*header)); if (ret < 0) goto err_out; if (ret < end - sizeof(*header)) { printf("premature end of image\n"); ret = -EIO; goto err_out; } if (swap) { void *ptr; for (ptr = zimage; ptr < zimage + end; ptr += 4) *(u32 *)ptr = swab32(*(u32 *)ptr); } ret = do_bootz_linux_fdt(fd, data); if (ret && ret != -ENXIO) goto err_out; close(fd); return __do_bootm_linux(data, swap); err_out: close(fd); return ret; }
// Read and discard specified number of bytes void Serial::eat(int n) { char buf[n] ; read_full(buf, n) ; }
/** * read_file_2 - read a file to an allocated buffer * @filename: The filename to read * @size: After successful return contains the size of the file * @outbuf: contains a pointer to the file data after successful return * @max_size: The maximum size to read. Use FILESIZE_MAX for reading files * of any size. * * This function reads a file to an allocated buffer. At maximum @max_size * bytes are read. The actual read size is returned in @size. -EFBIG is * returned if the file is bigger than @max_size, but the buffer is read * anyway up to @max_size in this case. Free the buffer with free() after * usage. * * Return: 0 for success, or negative error code. -EFBIG is returned * when the file has been bigger than max_size. */ int read_file_2(const char *filename, size_t *size, void **outbuf, loff_t max_size) { int fd; struct stat s; void *buf = NULL; const char *tmpfile = "/.read_file_tmp"; int ret; loff_t read_size; again: ret = stat(filename, &s); if (ret) return ret; if (max_size == FILESIZE_MAX) read_size = s.st_size; else read_size = max_size; if (read_size == FILESIZE_MAX) { ret = copy_file(filename, tmpfile, 0); if (ret) return ret; filename = tmpfile; goto again; } buf = calloc(read_size + 1, 1); if (!buf) { ret = -ENOMEM; goto err_out; } fd = open(filename, O_RDONLY); if (fd < 0) { ret = fd; goto err_out; } ret = read_full(fd, buf, read_size); if (ret < 0) goto err_out1; close(fd); if (size) *size = ret; if (filename == tmpfile) unlink(tmpfile); *outbuf = buf; if (read_size < s.st_size) return -EFBIG; return 0; err_out1: close(fd); err_out: free(buf); if (filename == tmpfile) unlink(tmpfile); return ret; }
int main(int argc, char **argv) { const char *progname = argv[0]; struct ndn *ndn = NULL; struct ndn_charbuf *name = NULL; struct ndn_charbuf *pname = NULL; struct ndn_charbuf *temp = NULL; struct ndn_charbuf *extopt = NULL; long expire = -1; int versioned = 0; size_t blocksize = 8*1024; int status = 0; int res; ssize_t read_res; unsigned char *buf = NULL; enum ndn_content_type content_type = NDN_CONTENT_DATA; struct ndn_closure in_interest = {.p=&incoming_interest}; const char *postver = NULL; const char *key_uri = NULL; int force = 0; int verbose = 0; int timeout = -1; int setfinal = 0; int prefixcomps = -1; int fd; struct ndn_signing_params sp = NDN_SIGNING_PARAMS_INIT; while ((res = getopt(argc, argv, "e:fhk:lvV:p:t:w:x:")) != -1) { switch (res) { case 'e': if (extopt == NULL) extopt = ndn_charbuf_create(); fd = open(optarg, O_RDONLY); if (fd < 0) { perror(optarg); exit(1); } for (;;) { read_res = read(fd, ndn_charbuf_reserve(extopt, 64), 64); if (read_res <= 0) break; extopt->length += read_res; } if (read_res < 0) perror(optarg); close(fd); break; case 'f': force = 1; break; case 'l': setfinal = 1; // set FinalBlockID to last comp of name break; case 'k': key_uri = optarg; break; case 'p': prefixcomps = atoi(optarg); if (prefixcomps < 0) usage(progname); break; case 'x': expire = atol(optarg); if (expire <= 0) usage(progname); break; case 'v': verbose = 1; break; case 'V': versioned = 1; postver = optarg; if (0 == memcmp(postver, "%00", 3)) setfinal = 1; break; case 'w': timeout = atol(optarg); if (timeout <= 0) usage(progname); timeout *= 1000; break; case 't': if (0 == strcasecmp(optarg, "DATA")) { content_type = NDN_CONTENT_DATA; break; } if (0 == strcasecmp(optarg, "ENCR")) { content_type = NDN_CONTENT_ENCR; break; } if (0 == strcasecmp(optarg, "GONE")) { content_type = NDN_CONTENT_GONE; break; } if (0 == strcasecmp(optarg, "KEY")) { content_type = NDN_CONTENT_KEY; break; } if (0 == strcasecmp(optarg, "LINK")) { content_type = NDN_CONTENT_LINK; break; } if (0 == strcasecmp(optarg, "NACK")) { content_type = NDN_CONTENT_NACK; break; } content_type = atoi(optarg); if (content_type > 0 && content_type <= 0xffffff) break; fprintf(stderr, "Unknown content type %s\n", optarg); /* FALLTHRU */ default: case 'h': usage(progname); break; } } argc -= optind; argv += optind; if (argv[0] == NULL) usage(progname); name = ndn_charbuf_create(); res = ndn_name_from_uri(name, argv[0]); if (res < 0) { fprintf(stderr, "%s: bad ndn URI: %s\n", progname, argv[0]); exit(1); } if (argv[1] != NULL) fprintf(stderr, "%s warning: extra arguments ignored\n", progname); /* Preserve the original prefix, in case we add versioning, * but trim it down if requested for the interest filter registration */ pname = ndn_charbuf_create(); ndn_charbuf_append(pname, name->buf, name->length); if (prefixcomps >= 0) { res = ndn_name_chop(pname, NULL, prefixcomps); if (res < 0) { fprintf(stderr, "%s: unable to trim name to %d component%s.\n", progname, prefixcomps, prefixcomps == 1 ? "" : "s"); exit(1); } } /* Connect to ndnd */ ndn = ndn_create(); if (ndn_connect(ndn, NULL) == -1) { perror("Could not connect to ndnd"); exit(1); } /* Read the actual user data from standard input */ buf = calloc(1, blocksize); read_res = read_full(0, buf, blocksize); if (read_res < 0) { perror("read"); read_res = 0; status = 1; } /* Tack on the version component if requested */ if (versioned) { res = ndn_create_version(ndn, name, NDN_V_REPLACE | NDN_V_NOW | NDN_V_HIGH, 0, 0); if (res < 0) { fprintf(stderr, "%s: ndn_create_version() failed\n", progname); exit(1); } if (postver != NULL) { res = ndn_name_from_uri(name, postver); if (res < 0) { fprintf(stderr, "-V %s: invalid name suffix\n", postver); exit(0); } } } temp = ndn_charbuf_create(); /* Ask for a FinalBlockID if appropriate. */ if (setfinal) sp.sp_flags |= NDN_SP_FINAL_BLOCK; if (res < 0) { fprintf(stderr, "Failed to create signed_info (res == %d)\n", res); exit(1); } /* Set content type */ sp.type = content_type; /* Set freshness */ if (expire >= 0) { if (sp.template_ndnb == NULL) { sp.template_ndnb = ndn_charbuf_create(); ndn_charbuf_append_tt(sp.template_ndnb, NDN_DTAG_SignedInfo, NDN_DTAG); } else if (sp.template_ndnb->length > 0) { sp.template_ndnb->length--; } ndnb_tagged_putf(sp.template_ndnb, NDN_DTAG_FreshnessSeconds, "%ld", expire); sp.sp_flags |= NDN_SP_TEMPL_FRESHNESS; ndn_charbuf_append_closer(sp.template_ndnb); } /* Set key locator, if supplied */ if (key_uri != NULL) { struct ndn_charbuf *c = ndn_charbuf_create(); res = ndn_name_from_uri(c, key_uri); if (res < 0) { fprintf(stderr, "%s is not a valid ndnx URI\n", key_uri); exit(1); } if (sp.template_ndnb == NULL) { sp.template_ndnb = ndn_charbuf_create(); ndn_charbuf_append_tt(sp.template_ndnb, NDN_DTAG_SignedInfo, NDN_DTAG); } else if (sp.template_ndnb->length > 0) { sp.template_ndnb->length--; } ndn_charbuf_append_tt(sp.template_ndnb, NDN_DTAG_KeyLocator, NDN_DTAG); ndn_charbuf_append_tt(sp.template_ndnb, NDN_DTAG_KeyName, NDN_DTAG); ndn_charbuf_append(sp.template_ndnb, c->buf, c->length); ndn_charbuf_append_closer(sp.template_ndnb); ndn_charbuf_append_closer(sp.template_ndnb); sp.sp_flags |= NDN_SP_TEMPL_KEY_LOCATOR; ndn_charbuf_append_closer(sp.template_ndnb); ndn_charbuf_destroy(&c); } if (extopt != NULL && extopt->length > 0) { if (sp.template_ndnb == NULL) { sp.template_ndnb = ndn_charbuf_create(); ndn_charbuf_append_tt(sp.template_ndnb, NDN_DTAG_SignedInfo, NDN_DTAG); } else if (sp.template_ndnb->length > 0) { sp.template_ndnb->length--; } ndnb_append_tagged_blob(sp.template_ndnb, NDN_DTAG_ExtOpt, extopt->buf, extopt->length); sp.sp_flags |= NDN_SP_TEMPL_EXT_OPT; ndn_charbuf_append_closer(sp.template_ndnb); } /* Create the signed content object, ready to go */ temp->length = 0; res = ndn_sign_content(ndn, temp, name, &sp, buf, read_res); if (res != 0) { fprintf(stderr, "Failed to encode ContentObject (res == %d)\n", res); exit(1); } if (read_res == blocksize) { read_res = read_full(0, buf, 1); if (read_res == 1) { fprintf(stderr, "%s: warning - truncated data\n", argv[0]); status = 1; } } free(buf); buf = NULL; if (force) { /* At user request, send without waiting to see an interest */ res = ndn_put(ndn, temp->buf, temp->length); if (res < 0) { fprintf(stderr, "ndn_put failed (res == %d)\n", res); exit(1); } } else { in_interest.data = temp; /* Set up a handler for interests */ res = ndn_set_interest_filter(ndn, pname, &in_interest); if (res < 0) { fprintf(stderr, "Failed to register interest (res == %d)\n", res); exit(1); } res = ndn_run(ndn, timeout); if (in_interest.intdata == 0) { if (verbose) fprintf(stderr, "Nobody's interested\n"); exit(1); } } if (verbose) { struct ndn_charbuf *uri = ndn_charbuf_create(); uri->length = 0; ndn_uri_append(uri, name->buf, name->length, 1); printf("wrote %s\n", ndn_charbuf_as_string(uri)); ndn_charbuf_destroy(&uri); } ndn_destroy(&ndn); ndn_charbuf_destroy(&name); ndn_charbuf_destroy(&pname); ndn_charbuf_destroy(&temp); ndn_charbuf_destroy(&sp.template_ndnb); ndn_charbuf_destroy(&extopt); exit(status); }
int mserv_create_connections(void){ char mserv_cmd[CHKPT_CMD_LEN]; char ack_message[CHKPT_MSG_LEN]; int n, i, ret = TLC_CHKPT_READY; if( (state_transfer_type == TLM)|| (state_transfer_type == TLM_STANDBY_DST)|| (state_transfer_type == TLM_STANDBY_SRCDST)|| (state_transfer_type == TLMC)|| (state_transfer_type == TLMC_STANDBY_DST) ){ if(mc_create_connection(1) < 0){ ret = TLC_CHKPT_ABORT; goto out_mserv_create_connection; } if(mc_create_connection2(1) < 0){ ret = TLC_CHKPT_ABORT; goto out_mserv_create_connection; } } else if( (state_transfer_type == TLM_EXEC)|| (state_transfer_type == TLC_EXEC)|| (state_transfer_type == TLC_TCP_STANDBY_DST) ){ printf("mserv_create_connection: chkpt type flag = %d\n", tlc_chkpt_type_flag); fflush(stdout); for(i = 0; i < chkpt_mserv_cnt; i++){ int base_port; if( (tlc_chkpt_type_flag == TLC_MEM_ADDR_CYCLIC_DOUBLE_CHANNELS) || (tlc_chkpt_type_flag == TLC_MEM_ADDR_BLOCK_DOUBLE_CHANNELS) ){ printf("mserv_create_connection: DOUBLE CHANNALS, port \n"); fflush(stdout); base_chkpt_mserv_client_fd[i] = socket(AF_INET, SOCK_STREAM, 0); base_chkpt_mserv_addr[i].sin_family = AF_INET; // base port = port + 100 printf(" TLC chkpt port[%d] = %d\n", i, tlc_chkpt_mserv_port[i]); fflush(stdout); if(tlc_chkpt_mserv_port[i] != 0){ base_port = tlc_chkpt_mserv_port[i]+100; printf(" 2 TLC chkpt base port = %d\n", base_port); fflush(stdout); base_chkpt_mserv_addr[i].sin_port = htons(base_port); } else{ base_chkpt_mserv_addr[i].sin_port = htons(MC_SERV_PORT + 100); } printf(" TLC mserv ip[%d] = %s\n", i, tlc_chkpt_mserv_ip[i]); fflush(stdout); if(tlc_chkpt_mserv_ip[i] != NULL){ printf(" 2 TLC mserv ip[%d] = %s\n", i, tlc_chkpt_mserv_ip[i]); fflush(stdout); base_chkpt_mserv_addr[i].sin_addr.s_addr = inet_addr(tlc_chkpt_mserv_ip[i]); } else{ base_chkpt_mserv_addr[i].sin_addr.s_addr = inet_addr(MC_SERV_IP_ADDR); } if(connect_w(base_chkpt_mserv_client_fd[i], (struct sockaddr *) &(base_chkpt_mserv_addr[i]), sizeof(base_chkpt_mserv_addr[i])) < 0){ printf("mserv_create_connection: Connect %d error 1 occured errno = %d\n", i, errno); base_chkpt_mserv_client_fd[i] = -1; ret = TLC_CHKPT_ABORT; break; } } // original connection chkpt_mserv_client_fd[i] = socket(AF_INET, SOCK_STREAM, 0); chkpt_mserv_addr[i].sin_family = AF_INET; if(tlc_chkpt_mserv_port[i] != 0){ //printf(" TLC chkpt base port[%d] = %d\n", i, tlc_chkpt_mserv_port[i]); chkpt_mserv_addr[i].sin_port = htons(tlc_chkpt_mserv_port[i]); } else{ chkpt_mserv_addr[i].sin_port = htons(MC_SERV_PORT); } if(tlc_chkpt_mserv_ip[i] != NULL){ //printf(" TLC mserv ip[%d] = %s\n", i, tlc_chkpt_mserv_ip[i]); chkpt_mserv_addr[i].sin_addr.s_addr = inet_addr(tlc_chkpt_mserv_ip[i]); } else{ chkpt_mserv_addr[i].sin_addr.s_addr = inet_addr(MC_SERV_IP_ADDR); } if(connect_w(chkpt_mserv_client_fd[i], (struct sockaddr *) &(chkpt_mserv_addr[i]), sizeof(chkpt_mserv_addr[i])) < 0){ printf("mserv_create_connection: Connect %d error 1 occured errno = %d\n", i, errno); chkpt_mserv_client_fd[i] = -1; ret = TLC_CHKPT_ABORT; break; } memset(mserv_cmd, 0, CHKPT_CMD_LEN); if( (tlc_chkpt_type_flag == TLC_MEM_ADDR_BLOCK) || (tlc_chkpt_type_flag == TLC_MEM_ADDR_BLOCK_DOUBLE_CHANNELS) ){ strncpy(mserv_cmd, "PUT ", CHKPT_CMD_LEN); mserv_init_command_block( chkpt_mserv_client_fd[i], mserv_cmd, mserv_allocate_pages[i], mserv_base_page_id[i], tlc_chkpt_name_str ); } else if( (tlc_chkpt_type_flag == TLC_MEM_ADDR_CYCLIC) || (tlc_chkpt_type_flag == TLC_MEM_ADDR_CYCLIC_DOUBLE_CHANNELS) ){ strncpy(mserv_cmd, "SEND", CHKPT_CMD_LEN); mserv_init_command_interleave( chkpt_mserv_client_fd[i], mserv_cmd, mserv_allocate_pages[i], i, tlc_chkpt_name_str ); } if( (tlc_chkpt_type_flag == TLC_MEM_ADDR_BLOCK_DOUBLE_CHANNELS) || (tlc_chkpt_type_flag == TLC_MEM_ADDR_CYCLIC_DOUBLE_CHANNELS) ){ // Receive message request from client memset(ack_message, 0, CHKPT_MSG_LEN); n = read_full(chkpt_mserv_client_fd[i],ack_message, CHKPT_ACK_LEN); if (n<0) { printf("Error Reading ACK Message"); exit(1); } printf("Received %s from %d connection\n",ack_message, i); fflush(stdout); } } if(ret == TLC_CHKPT_READY){ mserv_target_page_bits = tlc_target_page_bits(); } else mserv_target_page_bits = -1; // used (temporarily) by an assertion in mserv_set() } else{ printf("mserv_create_conn: unknown state transfer type = %d \n", state_transfer_type); ret = TLC_CHKPT_ABORT; } out_mserv_create_connection: return ret; }
static int file_contents_equal(const char *path1, const char *path2, ino_t *path2_inode_r) { struct stat st1, st2; int fd1, fd2, ret = -1; *path2_inode_r = 0; /* do a byte-by-byte comparison for the files to find out if they're the same or if this is a hash collision */ fd1 = open(path1, O_RDONLY); if (fd1 == -1) { if (errno != ENOENT) i_error("open(%s) failed: %m", path1); return -1; } fd2 = open(path2, O_RDONLY); if (fd1 == -1) { if (errno != ENOENT) i_error("open(%s) failed: %m", path2); i_close_fd(&fd1); return -1; } if (fstat(fd1, &st1) < 0) i_error("fstat(%s) failed: %m", path1); else if (fstat(fd2, &st2) < 0) i_error("fstat(%s) failed: %m", path1); else if (st1.st_size != st2.st_size) ret = 0; else { /* @UNSAFE: sizes match. compare. */ unsigned char buf1[IO_BLOCK_SIZE], buf2[IO_BLOCK_SIZE]; ssize_t ret1; int ret2; while ((ret1 = read(fd1, buf1, sizeof(buf1))) > 0) { if ((ret2 = read_full(fd2, buf2, ret1)) <= 0) { if (ret2 < 0) i_error("read(%s) failed: %m", path2); else ret = 0; break; } if (memcmp(buf1, buf2, ret1) != 0) { ret = 0; break; } } if (ret1 < 0) i_error("read(%s) failed: %m", path1); else if (ret1 == 0) ret = 1; *path2_inode_r = st2.st_ino; } if (close(fd1) < 0) i_error("close(%s) failed: %m", path1); if (close(fd2) < 0) i_error("close(%s) failed: %m", path2); return ret; }
int main(int argc, char **argv) { const char *progname = argv[0]; struct ccn *ccn = NULL; struct ccn_charbuf *root = NULL; struct ccn_charbuf *name = NULL; struct ccn_charbuf *temp = NULL; struct ccn_charbuf *templ = NULL; struct ccn_charbuf *signed_info = NULL; struct ccn_charbuf *keylocator = NULL; struct ccn_charbuf *finalblockid = NULL; struct ccn_keystore *keystore = NULL; long expire = -1; long blocksize = 1024; int i; int status = 0; int res; ssize_t read_res; unsigned char *buf = NULL; struct mydata mydata = { 0 }; struct ccn_closure in_content = {.p=&incoming_content, .data=&mydata}; struct ccn_closure in_interest = {.p=&incoming_interest, .data=&mydata}; while ((res = getopt(argc, argv, "hx:b:")) != -1) { switch (res) { case 'x': expire = atol(optarg); if (expire <= 0) usage(progname); break; case 'b': blocksize = atol(optarg); break; default: case 'h': usage(progname); break; } } argc -= optind; argv += optind; if (argv[0] == NULL) usage(progname); name = ccn_charbuf_create(); res = ccn_name_from_uri(name, argv[0]); if (res < 0) { fprintf(stderr, "%s: bad ccn URI: %s\n", progname, argv[0]); exit(1); } if (argv[1] != NULL) fprintf(stderr, "%s warning: extra arguments ignored\n", progname); ccn = ccn_create(); if (ccn_connect(ccn, NULL) == -1) { perror("Could not connect to ccnd"); exit(1); } buf = calloc(1, blocksize); root = name; name = ccn_charbuf_create(); temp = ccn_charbuf_create(); templ = ccn_charbuf_create(); signed_info = ccn_charbuf_create(); keystore = ccn_keystore_create(); temp->length = 0; ccn_charbuf_putf(temp, "%s/.ccnx/.ccnx_keystore", getenv("HOME")); res = ccn_keystore_init(keystore, ccn_charbuf_as_string(temp), "Th1s1sn0t8g00dp8ssw0rd."); if (res != 0) { printf("Failed to initialize keystore\n"); exit(1); } name->length = 0; ccn_charbuf_append(name, root->buf, root->length); /* Set up a handler for interests */ ccn_set_interest_filter(ccn, name, &in_interest); /* Initiate check to see whether there is already something there. */ temp->length = 0; ccn_charbuf_putf(temp, "%d", 0); ccn_name_append(name, temp->buf, temp->length); templ->length = 0; ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); ccn_charbuf_append_closer(templ); /* </Name> */ ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG); ccn_charbuf_append_tt(templ, 1, CCN_UDATA); ccn_charbuf_append(templ, "1", 1); ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */ // XXX - use pubid ccn_charbuf_append_closer(templ); /* </Interest> */ res = ccn_express_interest(ccn, name, &in_content, templ); if (res < 0) abort(); /* Construct a key locator contining the key itself */ keylocator = ccn_charbuf_create(); ccn_charbuf_append_tt(keylocator, CCN_DTAG_KeyLocator, CCN_DTAG); ccn_charbuf_append_tt(keylocator, CCN_DTAG_Key, CCN_DTAG); res = ccn_append_pubkey_blob(keylocator, ccn_keystore_public_key(keystore)); if (res < 0) ccn_charbuf_destroy(&keylocator); else { ccn_charbuf_append_closer(keylocator); /* </Key> */ ccn_charbuf_append_closer(keylocator); /* </KeyLocator> */ } for (i = 0;; i++) { read_res = read_full(0, buf, blocksize); if (read_res < 0) { perror("read"); read_res = 0; status = 1; } signed_info->length = 0; if (read_res < blocksize) { temp->length = 0; ccn_charbuf_putf(temp, "%d", i); ccn_name_append(name, temp->buf, temp->length); finalblockid = ccn_charbuf_create(); ccn_charbuf_append_tt(finalblockid, temp->length, CCN_BLOB); ccn_charbuf_append(finalblockid, temp->buf, temp->length); } res = ccn_signed_info_create(signed_info, /*pubkeyid*/ccn_keystore_public_key_digest(keystore), /*publisher_key_id_size*/ccn_keystore_public_key_digest_length(keystore), /*datetime*/NULL, /*type*/CCN_CONTENT_DATA, /*freshness*/ expire, finalblockid, keylocator); /* Put the keylocator in the first block only. */ ccn_charbuf_destroy(&keylocator); if (res < 0) { fprintf(stderr, "Failed to create signed_info (res == %d)\n", res); exit(1); } name->length = 0; ccn_charbuf_append(name, root->buf, root->length); temp->length = 0; ccn_charbuf_putf(temp, "%d", i); ccn_name_append(name, temp->buf, temp->length); temp->length = 0; ccn_charbuf_append(temp, buf, read_res); temp->length = 0; res = ccn_encode_ContentObject(temp, name, signed_info, buf, read_res, NULL, ccn_keystore_private_key(keystore)); if (res != 0) { fprintf(stderr, "Failed to encode ContentObject (res == %d)\n", res); exit(1); } if (i == 0) { /* Finish check for old content */ if (mydata.content_received == 0) ccn_run(ccn, 100); if (mydata.content_received > 0) { fprintf(stderr, "%s: name is in use: %s\n", progname, argv[0]); exit(1); } mydata.outstanding++; /* the first one is free... */ } res = ccn_put(ccn, temp->buf, temp->length); if (res < 0) { fprintf(stderr, "ccn_put failed (res == %d)\n", res); exit(1); } if (read_res < blocksize) break; if (mydata.outstanding > 0) mydata.outstanding--; else res = 10; res = ccn_run(ccn, res * 100); if (res < 0) { status = 1; break; } } free(buf); buf = NULL; ccn_charbuf_destroy(&root); ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&temp); ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&finalblockid); ccn_keystore_destroy(&keystore); ccn_destroy(&ccn); exit(status); }
int main(int argc, char **argv) { const char *progname = argv[0]; struct ndn *ndn = NULL; struct ndn_charbuf *root = NULL; struct ndn_charbuf *name = NULL; struct ndn_charbuf *temp = NULL; struct ndn_charbuf *templ = NULL; struct ndn_signing_params sp = NDN_SIGNING_PARAMS_INIT; long expire = -1; long blocksize = 1024; int i; int status = 0; int res; ssize_t read_res; unsigned char *buf = NULL; struct mydata mydata = { 0 }; struct ndn_closure in_content = {.p=&incoming_content, .data=&mydata}; struct ndn_closure in_interest = {.p=&incoming_interest, .data=&mydata}; while ((res = getopt(argc, argv, "hx:b:")) != -1) { switch (res) { case 'x': expire = atol(optarg); if (expire <= 0) usage(progname); break; case 'b': blocksize = atol(optarg); break; default: case 'h': usage(progname); break; } } argc -= optind; argv += optind; if (argc != 1) usage(progname); name = ndn_charbuf_create(); res = ndn_name_from_uri(name, argv[0]); if (res < 0) { fprintf(stderr, "%s: bad NDN URI: %s\n", progname, argv[0]); exit(1); } ndn = ndn_create(); if (ndn_connect(ndn, NULL) == -1) { perror("Could not connect to ndnd"); exit(1); } buf = calloc(1, blocksize); root = name; name = ndn_charbuf_create(); temp = ndn_charbuf_create(); templ = ndn_charbuf_create(); /* Set up a handler for interests */ ndn_charbuf_append(name, root->buf, root->length); ndn_set_interest_filter(ndn, name, &in_interest); /* Initiate check to see whether there is already something there. */ ndn_charbuf_reset(temp); ndn_charbuf_putf(temp, "%d", 0); ndn_name_append(name, temp->buf, temp->length); ndn_charbuf_reset(templ); ndnb_element_begin(templ, NDN_DTAG_Interest); ndnb_element_begin(templ, NDN_DTAG_Name); ndnb_element_end(templ); /* </Name> */ ndnb_tagged_putf(templ, NDN_DTAG_MaxSuffixComponents, "%d", 1); // XXX - use pubid ndnb_element_end(templ); /* </Interest> */ res = ndn_express_interest(ndn, name, &in_content, templ); if (res < 0) abort(); sp.freshness = expire; for (i = 0;; i++) { read_res = read_full(0, buf, blocksize); if (read_res < 0) { perror("read"); read_res = 0; status = 1; } if (read_res < blocksize) { sp.sp_flags |= NDN_SP_FINAL_BLOCK; } ndn_charbuf_reset(name); ndn_charbuf_append(name, root->buf, root->length); ndn_charbuf_reset(temp); ndn_charbuf_putf(temp, "%d", i); ndn_name_append(name, temp->buf, temp->length); ndn_charbuf_reset(temp); ndn_charbuf_append(temp, buf, read_res); ndn_charbuf_reset(temp); res = ndn_sign_content(ndn, temp, name, &sp, buf, read_res); if (res != 0) { fprintf(stderr, "Failed to sign ContentObject (res == %d)\n", res); exit(1); } /* Put the keylocator in the first block only. */ sp.sp_flags |= NDN_SP_OMIT_KEY_LOCATOR; if (i == 0) { /* Finish check for old content */ if (mydata.content_received == 0) ndn_run(ndn, 100); if (mydata.content_received > 0) { fprintf(stderr, "%s: name is in use: %s\n", progname, argv[0]); exit(1); } mydata.outstanding++; /* the first one is free... */ } res = ndn_put(ndn, temp->buf, temp->length); if (res < 0) { fprintf(stderr, "ndn_put failed (res == %d)\n", res); exit(1); } if (read_res < blocksize) break; if (mydata.outstanding > 0) mydata.outstanding--; else res = 10; res = ndn_run(ndn, res * 100); if (res < 0) { status = 1; break; } } free(buf); buf = NULL; ndn_charbuf_destroy(&root); ndn_charbuf_destroy(&name); ndn_charbuf_destroy(&temp); ndn_destroy(&ndn); exit(status); }
static int do_bootm_aimage(struct image_data *data) { struct resource *snd_stage_res; int fd, ret; struct android_header __header, *header; void *buf; int to_read; struct android_header_comp *cmp; unsigned long mem_free; unsigned long mem_start, mem_size; ret = sdram_start_and_size(&mem_start, &mem_size); if (ret) return ret; fd = open(data->os_file, O_RDONLY); if (fd < 0) { perror("open"); return 1; } header = &__header; ret = read(fd, header, sizeof(*header)); if (ret < sizeof(*header)) { printf("could not read %s\n", data->os_file); goto err_out; } printf("Android Image for '%s'\n", header->name); /* * As on tftp we do not support lseek and we will just have to seek * for the size of a page - 1 max just buffer instead to read to dummy * data */ buf = xmalloc(header->page_size); to_read = header->page_size - sizeof(*header); ret = read_full(fd, buf, to_read); if (ret < 0) { printf("could not read dummy %d from %s\n", to_read, data->os_file); goto err_out; } cmp = &header->kernel; data->os_res = request_sdram_region("akernel", cmp->load_addr, cmp->size); if (!data->os_res) { pr_err("Cannot request region 0x%08x - 0x%08x, using default load address\n", cmp->load_addr, cmp->size); data->os_address = mem_start + PAGE_ALIGN(cmp->size * 4); data->os_res = request_sdram_region("akernel", data->os_address, cmp->size); if (!data->os_res) { pr_err("Cannot request region 0x%08x - 0x%08x\n", cmp->load_addr, cmp->size); ret = -ENOMEM; goto err_out; } } ret = aimage_load_resource(fd, data->os_res, buf, header->page_size); if (ret < 0) { perror("could not read kernel"); goto err_out; } /* * fastboot always expect a ramdisk * in barebox we can be less restrictive */ cmp = &header->ramdisk; if (cmp->size) { data->initrd_res = request_sdram_region("ainitrd", cmp->load_addr, cmp->size); if (!data->initrd_res) { ret = -ENOMEM; goto err_out; } ret = aimage_load_resource(fd, data->initrd_res, buf, header->page_size); if (ret < 0) { perror("could not read initrd"); goto err_out; } } if (!getenv("aimage_noverwrite_bootargs")) linux_bootargs_overwrite(header->cmdline); if (!getenv("aimage_noverwrite_tags")) armlinux_set_bootparams((void*)header->tags_addr); cmp = &header->second_stage; if (cmp->size) { void (*second)(void); snd_stage_res = request_sdram_region("asecond", cmp->load_addr, cmp->size); if (!snd_stage_res) { ret = -ENOMEM; goto err_out; } ret = aimage_load_resource(fd, snd_stage_res, buf, header->page_size); if (ret < 0) { perror("could not read initrd"); goto err_out; } second = (void*)snd_stage_res->start; shutdown_barebox(); second(); restart_machine(); } close(fd); /* * Put devicetree right after initrd if present or after the kernel * if not. */ if (data->initrd_res) mem_free = PAGE_ALIGN(data->initrd_res->end); else mem_free = PAGE_ALIGN(data->os_res->end + SZ_1M); return __do_bootm_linux(data, mem_free, 0); err_out: linux_bootargs_overwrite(NULL); close(fd); return ret; }
int main(int argc, char *argv[]) { int j=0,n,count=0,count_send=0,count_allocate =0; int client_fd,server_fd,client_size; int sock,bytes_recieved,true = 1 ; struct sockaddr_in server_addr,client_addr; int sin_size,bytes_send=0; int servno,num_serv, port_num; char message[5]; uint64_t total_pages,total_pages_bck; ram_addr_t adr,k,index=0,flags,pageid; mempage **values; if(argc == 3){ num_serv = atoi(argv[1]); port_num = atoi(argv[2]); printf(" num_serv = %d port_num = %d\n", num_serv, port_num); fflush(stdout); } else{ printf(" invalid parameter \n"); exit(1); } ///////////////////////////////////////////////// // Create a socket ///////////////////////////////////////////////// if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Socket"); exit(1); } if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1) { perror("Setsockopt"); exit(1); } /////////////////////////////////////////////// // Identify IP address and Port ////////////////////////////////////////////// server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port_num); server_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(server_addr.sin_zero),8); ////////////////////////////////////////////// // Bind an IP address to the socket ////////////////////////////////////////////// if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))== -1) { perror("Unable to bind"); exit(1); } ///////////////////////////////////////////// // Specify a size of a request queue ///////////////////////////////////////////// if (listen(sock, 5) == -1) { perror("Listen"); exit(1); } printf("\nTCPServer Waiting for client\n"); fflush(stdout); ///////////////////////////////////////////////// // Main Server Loop - accept and handle request ///////////////////////////////////////////////// // printf("\nServer up and running \n"); // printf("TARGET_PAGE_SIZE=%d ,TARGET_PAGE_MASK=%"PRIx64"\n",TARGET_PAGE_SIZE,TARGET_PAGE_MASK); while(1) { sin_size = sizeof(client_addr); connected = accept_cr(sock, (struct sockaddr *)&client_addr,&sin_size); if (connected < 0) { printf("Error Accept\n"); exit(1); } printf("\n I got a connection from (%s , %d)\n", inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); fflush(stdout); gettimeofday(&startsave, NULL); // Start timing startret = startsave; //////////////////////////////////////////// // Receive message request from client /////////////////////////////////////////// //n=recv(connected,message,4,0); n=read_full(connected,message,4); if (n<0) { printf("Error Reading Request Message"); exit(1); } printf("Received Message Request is %s , %d \n",message,n); fflush(stdout); //////////////////////////////////////////// // Received Total Pages No. //////////////////////////////////////////// n=read_full(connected,&total_pages,sizeof(total_pages)); if (n<0) { printf("Error Reading Total Pages"); exit(1); } printf("Received Total Pages= %d \n",total_pages); // printf("MY_TARGET_PAGE_MASK=%"PRIx64"",MY_TARGET_PAGE_MASK); fflush(stdout); ////////////////////////////////////////// // Received Total Pages No. //////////////////////////////////////////// n=read_full(connected,&servno,sizeof(servno)); if (n<0) { printf("Error Reading Total Pages"); exit(1); } //////////////////////////////////////////// // Handle message request //////////////////////////////////////////// if(strcmp(message,"SEND")==0){ count=0 ; values = (mempage*) calloc (total_pages,sizeof(mempage)); total_pages_bck = total_pages ;//Keep for Backup data case "SEND" // printf("total_pages_bck = %d\n",total_pages_bck); // fflush(stdout); // memset(values,NULL,sizeof(values)); /////////////////////////////////////////// // Control loop for received addr, data-pages // 1.Recv addr from client-side /////////////////////////////////////////// // bytes_recieved =recv(connected,&adr,sizeof(adr),0); bytes_recieved =read_full(connected,&adr,sizeof(adr)); // printf("Rec.adr Before convert =%"PRIx64"\n",adr); if (bytes_recieved == -1) { fprintf(stderr,"recv addr failed :%s\n",strerror(errno)); exit(1); } // printf("Rec.adr=%"PRIx64" \n",adr); // fflush(stdout); flags = adr &~TARGET_PAGE_MASK ; adr &= TARGET_PAGE_MASK ; // printf("First After convert adr=%"PRIx64" ,flags=%"PRIx64" ,MY_EOF=%"PRIx64" ,TARGET_PAGE_MASK=%"PRIx64"\n",adr,flags,MY_EOF,TARGET_PAGE_MASK); // fflush(stdout); while(flags != MY_EOF){ count++; index = adr >> TARGET_PAGE_BITS ; index = index/num_serv ; // printf("index/2 = %d \n",index/2); /////////////////////////////////////////// // 2.Recv data-page /////////////////////////////////////////// if(*(values+index)==NULL){ values[index]=(mempage*)malloc(sizeof(mempage)); count_allocate++; } // memset(values[index],0,sizeof(values)); // bytes_recieved =recv(connected,values[index],sizeof(mempage),0); bytes_recieved =read_full(connected,values[index],sizeof(mempage)); if (bytes_recieved == -1) { fprintf(stderr,"recv values failed :%s\n",strerror(errno)); exit(1); } // printf("Rec.adr of values=%"PRIx64" \n",&values[index]); // fflush(stdout); // bytes_recieved =recv(connected,&adr,sizeof(adr),0); bytes_recieved =read_full(connected,&adr,sizeof(adr)); if (bytes_recieved == -1) { fprintf(stderr,"recv addr failed :%s\n",strerror(errno)); exit(1); } flags = adr &~TARGET_PAGE_MASK ; // printf("flags After =%"PRIx64"\n",flags); // fflush(stdout); // printf("Address before convert = %"PRIx64" ",adr); adr &= TARGET_PAGE_MASK ; // printf("n:%d After convert adr=%"PRIx64" ,flags=%"PRIx64" ,MY_EOF=%"PRIx64"\n",count ,adr,flags,MY_EOF); // fflush(stdout); }//end while MY_EOF gettimeofday(&endmemsave, NULL); memsave_elasp = ((endmemsave.tv_sec * 1000000 + endmemsave.tv_usec)-(startsave.tv_sec * 1000000 + startsave.tv_usec)); printf("Elasp time saving pages from VM to memory = %"PRId64" ms\n", (uint64_t)(memsave_elasp/1000)); printf("count recv = %d ,count_allocate = %d \n",count,count_allocate); fflush(stdout); /* n=0; for(k=0;k<total_pages;k++){ if(*(values+k)!= NULL){ n++; adr = k*TARGET_PAGE_SIZE; printf("n=%d k=%"PRIx64" values[k]=%"PRIx64" ,adr=%"PRIx64"\n",n,k,values[k],adr); fflush(stdout); } } */ //RECV }else { count_send=0;
static int do_bootm_aimage(struct image_data *data) { struct resource *snd_stage_res; int fd, ret; struct android_header __header, *header; void *buf; int to_read; struct android_header_comp *cmp; fd = open(data->os_file, O_RDONLY); if (fd < 0) { perror("open"); return 1; } header = &__header; ret = read(fd, header, sizeof(*header)); if (ret < sizeof(*header)) { printf("could not read %s\n", data->os_file); goto err_out; } printf("Android Image for '%s'\n", header->name); /* * As on tftp we do not support lseek and we will just have to seek * for the size of a page - 1 max just buffer instead to read to dummy * data */ buf = xmalloc(header->page_size); to_read = header->page_size - sizeof(*header); ret = read_full(fd, buf, to_read); if (ret < 0) { printf("could not read dummy %d from %s\n", to_read, data->os_file); goto err_out; } cmp = &header->kernel; data->os_res = request_sdram_region("akernel", cmp->load_addr, cmp->size); if (!data->os_res) { ret = -ENOMEM; goto err_out; } ret = aimage_load_resource(fd, data->os_res, buf, header->page_size); if (ret < 0) { perror("could not read kernel"); goto err_out; } /* * fastboot always expect a ramdisk * in barebox we can be less restrictive */ cmp = &header->ramdisk; if (cmp->size) { data->initrd_res = request_sdram_region("ainitrd", cmp->load_addr, cmp->size); if (!data->initrd_res) { ret = -ENOMEM; goto err_out; } ret = aimage_load_resource(fd, data->initrd_res, buf, header->page_size); if (ret < 0) { perror("could not read initrd"); goto err_out; } } if (!getenv("aimage_noverwrite_bootargs")) linux_bootargs_overwrite(header->cmdline); if (!getenv("aimage_noverwrite_tags")) armlinux_set_bootparams((void*)header->tags_addr); cmp = &header->second_stage; if (cmp->size) { void (*second)(void); snd_stage_res = request_sdram_region("asecond", cmp->load_addr, cmp->size); if (!snd_stage_res) { ret = -ENOMEM; goto err_out; } ret = aimage_load_resource(fd, snd_stage_res, buf, header->page_size); if (ret < 0) { perror("could not read initrd"); goto err_out; } second = (void*)snd_stage_res->start; shutdown_barebox(); second(); reset_cpu(0); } close(fd); return __do_bootm_linux(data, 0); err_out: linux_bootargs_overwrite(NULL); close(fd); return ret; }
static int auth_token_read_secret(const char *path, unsigned char secret_r[AUTH_TOKEN_SECRET_LEN]) { struct stat st, lst; int fd, ret; fd = open(path, O_RDONLY); if (fd == -1) { if (errno != ENOENT) i_error("open(%s) failed: %m", path); return -1; } if (fstat(fd, &st) < 0) { i_error("fstat(%s) failed: %m", path); i_close_fd(&fd); return -1; } /* check secret len and file type */ if (st.st_size != AUTH_TOKEN_SECRET_LEN || !S_ISREG(st.st_mode)) { i_error("Corrupted token secret file: %s", path); i_close_fd(&fd); if (unlink(path) < 0) i_error("unlink(%s) failed: %m", path); return -1; } /* verify that we're not dealing with a symbolic link */ if (lstat(path, &lst) < 0) { i_error("lstat(%s) failed: %m", path); i_close_fd(&fd); return -1; } /* check security parameters for compromise */ if ((st.st_mode & 07777) != 0600 || st.st_uid != geteuid() || st.st_nlink > 1 || !S_ISREG(lst.st_mode) || st.st_ino != lst.st_ino || !CMP_DEV_T(st.st_dev, lst.st_dev)) { i_error("Compromised token secret file: %s", path); i_close_fd(&fd); if (unlink(path) < 0) i_error("unlink(%s) failed: %m", path); return -1; } /* FIXME: fail here to generate new secret if stored one is too old */ ret = read_full(fd, secret_r, AUTH_TOKEN_SECRET_LEN); if (ret < 0) i_error("read(%s) failed: %m", path); else if (ret == 0) { i_error("Token secret file unexpectedly shrank: %s", path); ret = -1; } if (close(fd) < 0) i_error("close(%s) failed: %m", path); if (global_auth_settings->debug) i_debug("Read auth token secret from %s", path); return ret; }
void *uimage_load_to_buf(struct uimage_handle *handle, int image_no, size_t *outsize) { u32 size; int ret; struct uimage_handle_data *ihd; char ftbuf[128]; enum filetype ft; void *buf; if (image_no >= handle->nb_data_entries) return NULL; ihd = &handle->ihd[image_no]; ret = lseek(handle->fd, ihd->offset + handle->data_offset, SEEK_SET); if (ret < 0) return NULL; if (handle->header.ih_comp == IH_COMP_NONE) { buf = malloc(ihd->len); if (!buf) return NULL; ret = read_full(handle->fd, buf, ihd->len); if (ret < ihd->len) { free(buf); return NULL; } size = ihd->len; goto out; } ret = read(handle->fd, ftbuf, 128); if (ret < 0) return NULL; ft = file_detect_type(ftbuf, 128); if ((int)ft < 0) return NULL; if (ft != filetype_gzip) return NULL; ret = lseek(handle->fd, ihd->offset + handle->data_offset + ihd->len - 4, SEEK_SET); if (ret < 0) return NULL; ret = read(handle->fd, &size, 4); if (ret < 0) return NULL; size = le32_to_cpu(size); ret = lseek(handle->fd, ihd->offset + handle->data_offset, SEEK_SET); if (ret < 0) return NULL; buf = malloc(size); ret = uncompress_fd_to_buf(handle->fd, buf, uncompress_err_stdout); if (ret) { free(buf); return NULL; } out: if (outsize) *outsize = size; return buf; }
static int do_bootz_linux(struct image_data *data) { int fd, ret, swap = 0; struct zimage_header __header, *header; void *zimage; u32 end, start; size_t image_size; unsigned long load_address = data->os_address; unsigned long mem_start, mem_size, mem_free; ret = sdram_start_and_size(&mem_start, &mem_size); if (ret) return ret; fd = open(data->os_file, O_RDONLY); if (fd < 0) { perror("open"); return 1; } header = &__header; ret = read(fd, header, sizeof(*header)); if (ret < sizeof(*header)) { printf("could not read %s\n", data->os_file); goto err_out; } switch (header->magic) { case swab32(ZIMAGE_MAGIC): swap = 1; /* fall through */ case ZIMAGE_MAGIC: break; default: printf("invalid magic 0x%08x\n", header->magic); ret = -EINVAL; goto err_out; } end = header->end; start = header->start; if (swap) { end = swab32(end); start = swab32(start); } image_size = end - start; if (load_address == UIMAGE_INVALID_ADDRESS) { /* * Just use a conservative default of 4 times the size of the * compressed image, to avoid the need for the kernel to * relocate itself before decompression. */ data->os_address = mem_start + PAGE_ALIGN(image_size * 4); load_address = data->os_address; if (bootm_verbose(data)) printf("no OS load address, defaulting to 0x%08lx\n", load_address); } data->os_res = request_sdram_region("zimage", load_address, image_size); if (!data->os_res) { pr_err("bootm/zImage: failed to request memory at 0x%lx to 0x%lx (%d).\n", load_address, load_address + image_size, image_size); ret = -ENOMEM; goto err_out; } zimage = (void *)data->os_res->start; memcpy(zimage, header, sizeof(*header)); ret = read_full(fd, zimage + sizeof(*header), image_size - sizeof(*header)); if (ret < 0) goto err_out; if (ret < end - sizeof(*header)) { printf("premature end of image\n"); ret = -EIO; goto err_out; } if (swap) { void *ptr; for (ptr = zimage; ptr < zimage + end; ptr += 4) *(u32 *)ptr = swab32(*(u32 *)ptr); } ret = do_bootz_linux_fdt(fd, data); if (ret && ret != -ENXIO) goto err_out; close(fd); /* * put oftree/initrd close behind compressed kernel image to avoid * placing it outside of the kernels lowmem. */ mem_free = PAGE_ALIGN(data->os_res->end + SZ_1M); return __do_bootm_linux(data, mem_free, swap); err_out: close(fd); return ret; }
int main(int argc, char *argv[]) { int sock,bytes_recieved,flagtrue = 1 ; ram_addr_t adr,k,index=0,flags,pageid; int j=0,n,count=0,count_send=0; int client_fd,server_fd,client_size; struct sockaddr_in server_addr,client_addr; int sin_size,bytes_send=0; char message[5]; if(argc == 5){ num_serv = atoi(argv[1]); port_num = atoi(argv[2]); total_pages_param = atoi(argv[3]); chkpt_filename = argv[4]; printf(" restoring num_serv = %d port_num = %d total_pages_param = %" PRId64 " chkpt_filename = %s\n", num_serv, port_num, total_pages_param, chkpt_filename); fflush(stdout); } else{ printf(" invalid parameter! command must be in the format of \n ./restore-interleave num_serv port_num total_pages chkpt_filename\n"); exit(1); } /* Initialize mutex and condition variable objects */ pthread_mutex_init(&mutex, NULL); pthread_cond_init (&read_file_cond, NULL); /* For portability, explicitly create threads in a joinable state */ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_create(&thread, &attr, (void *) &read_chk_file, (void *) NULL); // Create a socket if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Socket"); exit(1); } if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&flagtrue,sizeof(int)) == -1) { perror("Setsockopt"); exit(1); } // Identify IP address and Port server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port_num); server_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(server_addr.sin_zero),8); // Bind an IP address to the socket if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))== -1) { perror("Unable to bind"); exit(1); } // Specify a size of a request queue if (listen(sock, 5) == -1) { perror("Listen"); exit(1); } printf("\nRecovery CYCLIC MEM Server Waiting for client\n"); fflush(stdout); // Main Server Loop - accept and handle request while(1){ sin_size = sizeof(client_addr); connected = accept_cr(sock, (struct sockaddr *)&client_addr,&sin_size); if (connected < 0) { printf("Error Accept\n"); exit(1); } printf("\n I got a connection from (%s , %d)\n", inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); fflush(stdout); gettimeofday(&startsave, NULL); // Start timing startret = startsave; // Receive message request from client n=read_full(connected,message, REQ_CMD_LEN); // SEND/RECV is expected if (n<0) { printf("Error Reading Request Message"); exit(1); } printf("Received Message Request is %s , %d \n",message,n); fflush(stdout); // Received Total Pages No. n=read_full(connected,&total_pages,sizeof(total_pages)); if (n<0) { printf("Error Reading Total Pages"); exit(1); } printf("Received Total Pages= %" PRId64 " and Total Pages Param= %" PRId64 " \n",total_pages, total_pages_param); fflush(stdout); // Received Total Pages No. n=read_full(connected,&servno,sizeof(servno)); if (n<0) { printf("Error Reading serv no"); exit(1); } // Handle requesting command if(strcmp(message,REQ_CMD)==0){ // sync with read_chk_file() pthread_mutex_lock(&mutex); while (!read_file_done){ pthread_cond_wait(&read_file_cond , &mutex); } pthread_mutex_unlock(&mutex); gettimeofday(&startmemret, NULL); for(k=0;k<total_pages;k++){ //1.send addr //2.send values if(*(values+k)!=NULL){ pageid = (k*num_serv)+servno;//change variable adr = pageid*TARGET_PAGE_SIZE; // printf("k=%d ,adr=%"PRIx64"\n",k,adr); bytes_send = write_full(connected,&adr,sizeof(adr)); if (bytes_send == -1) { fprintf(stderr,"send adr failed :%s\n",strerror(errno)); exit(1); }//end if bytes_send = write_full(connected,values[k],sizeof(mempage)); if (bytes_send == -1) { fprintf(stderr,"send values[k] failed :%s\n",strerror(errno)); exit(1); }//end if count_send++; // printf("n=%d k=%"PRIx64" ,adr=%"PRIx64"\n",count_send,k,adr); // fflush(stdout); } //end if NULL }//end for k memserver_send_eof(); printf("file count_send = %d\n",count_send); fflush(stdout); close(connected); gettimeofday(&endmemret, NULL); memret_elasp = ((endmemret.tv_sec * 1000000 + endmemret.tv_usec)-(startmemret.tv_sec * 1000000 + startmemret.tv_usec)); printf("Elasp time sending mem pages to recovery VM = %"PRId64" ms\n", (uint64_t)(memret_elasp/1000)); } } //end while(1) return 0; }
int main(int argc, char *argv[]) { int j=0,n,count=0,count_send=0,count_allocate =0; int client_fd,server_fd,client_size; int sock,bytes_recieved,true = 1 ; struct sockaddr_in server_addr,client_addr; int sin_size,bytes_send=0; int num_serv, port_num; char message[5]; ram_addr_t adr,k,index=0,flags,pageid; mempage **values = NULL; if(argc == 5){ num_serv = atoi(argv[1]); port_num = atoi(argv[2]); chkpt_filename = argv[3]; vm_name = argv[4]; printf(" num_serv = %d port_num = %d chkpt_filename = %s vm_name = %s\n", num_serv, port_num, chkpt_filename, vm_name); fflush(stdout); } else{ printf(" invalid parameter \n"); exit(1); } // Create a socket if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Socket"); exit(1); } if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1) { perror("Setsockopt"); exit(1); } // Identify IP address and Port server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port_num); server_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(server_addr.sin_zero),8); // Bind an IP address to the socket if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))== -1) { perror("Unable to bind"); exit(1); } // Specify a size of a request queue if (listen(sock, 5) == -1) { perror("Listen"); exit(1); } printf("\nCYCLIC MEM Server Waiting for client\n"); fflush(stdout); // Main Server Loop - accept and handle request while(1){ sin_size = sizeof(client_addr); connected = accept_cr(sock, (struct sockaddr *)&client_addr,&sin_size); if (connected < 0) { printf("Error Accept\n"); exit(1); } printf("\n I got a connection from (%s , %d)\n", inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); fflush(stdout); gettimeofday(&startsave, NULL); // Start timing startret = startsave; // Receive message request from client n=read_full(connected,message, SEND_BLOCK_REQ_CMD_LEN); // SEND/RECV is expected if (n<0) { printf("Error Reading Request Message"); exit(1); } printf("Received Message Request is %s , %d \n",message,n); fflush(stdout); // Received Total Pages No. n=read_full(connected,&base_page_id,sizeof(base_page_id)); if (n<0) { printf("Error Reading Total Pages"); exit(1); } printf("Received base page id= %" PRId64 " \n",base_page_id); fflush(stdout); // Received Total Pages No. n=read_full(connected,&total_pages,sizeof(total_pages)); if (n<0) { printf("Error Reading Total Pages"); exit(1); } printf("Received Total Pages= %" PRId64 " \n",total_pages); fflush(stdout); // Handle requesting command if(strcmp(message,SEND_BLOCK_REQ_CMD)==0){ count=0 ; values = (mempage**) calloc (total_pages,sizeof(mempage *)); total_pages_bck = total_pages ;//Keep for Backup data case "SEND" // printf("total_pages_bck = %d\n",total_pages_bck); // fflush(stdout); // Control loop for received addr, data-pages // 1.Recv addr from client-side bytes_recieved =read_full(connected,&adr,sizeof(adr)); // printf("Rec.adr Before convert =%"PRIx64"\n",adr); if (bytes_recieved == -1) { fprintf(stderr,"recv addr failed :%s\n",strerror(errno)); exit(1); } // printf("Rec.adr=%"PRIx64" \n",adr); // fflush(stdout); flags = adr &~TARGET_PAGE_MASK ; adr &= TARGET_PAGE_MASK ; // printf("First After convert adr=%"PRIx64" ,flags=%"PRIx64" ,MY_EOF=%"PRIx64" ,TARGET_PAGE_MASK=%"PRIx64"\n",adr,flags,MY_EOF,TARGET_PAGE_MASK); // fflush(stdout); while(flags != MY_EOF){ count++; index = adr >> TARGET_PAGE_BITS ; index = index - base_page_id; // define storage location // printf("index/2 = %d \n",index/2); // 2.Recv data-page if(*(values+index)==NULL){ values[index]=(mempage*)malloc(sizeof(mempage)); count_allocate++; } bytes_recieved =read_full(connected,values[index],sizeof(mempage)); if (bytes_recieved == -1) { fprintf(stderr,"recv values failed :%s\n",strerror(errno)); exit(1); } // printf("Rec.adr of values=%"PRIx64" \n",&values[index]); // fflush(stdout); bytes_recieved =read_full(connected,&adr,sizeof(adr)); if (bytes_recieved == -1) { fprintf(stderr,"recv addr failed :%s\n",strerror(errno)); exit(1); } flags = adr &~TARGET_PAGE_MASK ; // printf("flags After =%"PRIx64"\n",flags); // printf("Address before convert = %"PRIx64" ",adr); // fflush(stdout); adr &= TARGET_PAGE_MASK ; // printf("n:%d After convert adr=%"PRIx64" ,flags=%"PRIx64" ,MY_EOF=%"PRIx64"\n",count ,adr,flags,MY_EOF); // fflush(stdout); } //end while MY_EOF printf("count recieved pages = %d ,count allocated pages = %d \n",count,count_allocate); fflush(stdout); gettimeofday(&endmemsave, NULL); memsave_elasp = ((endmemsave.tv_sec * 1000000 + endmemsave.tv_usec)-(startsave.tv_sec * 1000000 + startsave.tv_usec)); printf("Elasp time saving pages from VM to memory = %"PRId64" ms\n", (uint64_t)(memsave_elasp/1000)); // write to a file //char chkptfilename[CHKPT_NAME_LEN]; //sprintf(chkptfilename, "vm%d.chk\0", base_page_id); printf("write to chkptfile %s\n", chkpt_filename); fflush(stdout); int file_fd; if((file_fd = open(chkpt_filename, O_CREAT | O_RDWR, 0666))<0){ printf("mem server: cannot open file error no = %d\n", errno); fflush(stdout); //exit(1); } for(k=0;k<total_pages;k++){ //1.send addr //2.send values if(*(values+k)!=NULL){ pageid = base_page_id+k; adr = pageid*TARGET_PAGE_SIZE; // change index to addr // printf("k=%d ,adr=%"PRIx64"\n",k,adr); bytes_send = write_full(file_fd,&adr,sizeof(adr)); if (bytes_send == -1) { fprintf(stderr,"send adr failed :%s\n",strerror(errno)); exit(1); } //end if bytes_send = write_full(file_fd,values[k],sizeof(mempage)); if (bytes_send == -1) { fprintf(stderr,"send values[k] failed :%s\n",strerror(errno)); exit(1); } //end if count_send++; // printf("n=%d k=%"PRIx64" ,adr=%"PRIx64"\n",count_send,k,adr); // fflush(stdout); } //end if NULL } adr = 0; adr= (adr|MY_EOF); bytes_send = write_full(file_fd,&adr,sizeof(adr)); close(file_fd); create_restoration_script(total_pages); gettimeofday(&endsave, NULL); filesave_elasp = ((endsave.tv_sec * 1000000 + endsave.tv_usec)-(endmemsave.tv_sec * 1000000 + endmemsave.tv_usec)); printf("Elasp time saving pages from memory to file= %"PRId64" ms\n", (uint64_t)(filesave_elasp/1000)); printf("count send to file = %d \n",count_send); fflush(stdout); } else{ // RECV request
static int do_bootz_linux(struct image_data *data) { int fd, ret, swap = 0; struct zimage_header __header, *header; void *zimage; u32 end, start; size_t image_size; unsigned long load_address = data->os_address; unsigned long mem_free; fd = open(data->os_file, O_RDONLY); if (fd < 0) { perror("open"); return 1; } header = &__header; ret = read(fd, header, sizeof(*header)); if (ret < sizeof(*header)) { printf("could not read %s\n", data->os_file); goto err_out; } switch (header->magic) { case swab32(ZIMAGE_MAGIC): swap = 1; /* fall through */ case ZIMAGE_MAGIC: break; default: printf("invalid magic 0x%08x\n", header->magic); ret = -EINVAL; goto err_out; } end = header->end; start = header->start; if (swap) { end = swab32(end); start = swab32(start); } image_size = end - start; load_address = data->os_address; ret = get_kernel_addresses(image_size, bootm_verbose(data), &load_address, &mem_free); if (ret) return ret; data->os_res = request_sdram_region("zimage", load_address, image_size); if (!data->os_res) { pr_err("bootm/zImage: failed to request memory at 0x%lx to 0x%lx (%d).\n", load_address, load_address + image_size, image_size); ret = -ENOMEM; goto err_out; } zimage = (void *)data->os_res->start; memcpy(zimage, header, sizeof(*header)); ret = read_full(fd, zimage + sizeof(*header), image_size - sizeof(*header)); if (ret < 0) goto err_out; if (ret < image_size - sizeof(*header)) { printf("premature end of image\n"); ret = -EIO; goto err_out; } if (swap) { void *ptr; for (ptr = zimage; ptr < zimage + end; ptr += 4) *(u32 *)ptr = swab32(*(u32 *)ptr); } ret = do_bootz_linux_fdt(fd, data); if (ret && ret != -ENXIO) goto err_out; close(fd); return __do_bootm_linux(data, mem_free, swap); err_out: close(fd); return ret; }
/* * when data is sent over the wire, tcp takes care of reading the bits in * network order. dont need to htonl rrf->cntl before writing and after * reading from the network. * * FIXME: We are currently deciding whether to use marker or CRC based on * initiator's criteria or responder's reply, mainly to work with ammasso * card. This is not according to rfc and has to be changed. One way to do it * would be to have a config file, which takes into account each card type's * requirements, and pass it on to program as one argument. */ int mpa_init_startup(iwsk_t *iwsk, bool_t is_initiator, const char *pd_in, char *pd_out, pd_len_t rpd_len) { int ret; int len; pd_len_t pd_in_len; struct { char key[16]; uint32_t cntl; char init_string[0]; } *rrf; if(!strlen(pd_in)) pd_in_len = 0; else pd_in_len = strlen(pd_in) + 1; pd_len_t pd_out_len = 0; if (!pd_out || !pd_in) return -EINVAL; len = pd_in_len; len += sizeof(*rrf); rrf = Malloc(len); if (is_initiator) { memset(rrf, 0, len); memcpy(rrf->key, &MPA_REQ_KEY, sizeof(rrf->key)); if (iwsk->mpask.use_mrkr) mpa_set_M(rrf->cntl); /* we prefer markers */ if (iwsk->mpask.use_crc) mpa_set_C(rrf->cntl); /* we prefer CRC */ mpa_unset_R(rrf->cntl); mpa_set_Res(rrf->cntl); mpa_set_Rev(rrf->cntl, 0); /* ammasso specific revision */ mpa_set_PD_Length(rrf->cntl, pd_in_len); strcpy(rrf->init_string, pd_in); ret = write_full(iwsk->sk, rrf, len); if (ret < 0) return ret; read_full(iwsk->sk, rrf, sizeof(*rrf)); pd_out_len = mpa_get_PD_Length(rrf->cntl); /* XXX: We dont test if pd_out_len > pd_in_len and reallocate rrf * since we read into pd_out instead of copying from * rrf->init_string */ if (pd_out_len > rpd_len) return -EMSGSIZE; read_full(iwsk->sk, pd_out, pd_out_len); /* FIXME: change this to confirm to spec */ if (mpa_get_M(rrf->cntl)) { iwsk->mpask.use_mrkr = TRUE; } else { iwsk->mpask.use_mrkr = FALSE; } if (mpa_get_C(rrf->cntl)) { iwsk->mpask.use_crc = TRUE; } else { iwsk->mpask.use_crc = FALSE; } /* TODO: Surface these errors */ if (memcmp(&(rrf->key), &(MPA_REP_KEY), sizeof(rrf->key))) return -EBADMSG; } else { read_full(iwsk->sk, rrf, sizeof(*rrf)); /* FIXME: change this to confirm to spec */ if (iwsk->mpask.use_mrkr == 0) { if (mpa_get_M(rrf->cntl)) { iwsk->mpask.use_mrkr = TRUE; } else { iwsk->mpask.use_mrkr = FALSE; } } else { if (!mpa_get_M(rrf->cntl)) return -EBADMSG; } if (iwsk->mpask.use_crc == 0) { if (mpa_get_C(rrf->cntl)) { iwsk->mpask.use_crc = TRUE; } else { iwsk->mpask.use_crc = FALSE; } } else { if (!mpa_get_C(rrf->cntl)) return -EBADMSG; } pd_out_len = mpa_get_PD_Length(rrf->cntl); /* XXX: We dont test if pd_out_len > pd_in_len and reallocate rrf * since we read into pd_out instead of copying from * rrf->init_string */ read_full(iwsk->sk, pd_out, pd_out_len); /* TODO: Surface these errors */ if (memcmp(&(rrf->key), &(MPA_REQ_KEY), sizeof(rrf->key))) return -EBADMSG; memset(rrf, 0, len); memcpy(&(rrf->key), &MPA_REP_KEY, sizeof(rrf->key)); if (iwsk->mpask.use_mrkr) { mpa_set_M(rrf->cntl); } else { mpa_unset_M(rrf->cntl); } if (iwsk->mpask.use_crc) { mpa_set_C(rrf->cntl); } else { mpa_unset_C(rrf->cntl); } mpa_unset_R(rrf->cntl); mpa_set_Res(rrf->cntl); mpa_set_Rev(rrf->cntl, 1); /* neteffect specific revision */ mpa_set_PD_Length(rrf->cntl, pd_in_len); if(pd_in_len != 0) strcpy(rrf->init_string, pd_in); ret = write_full(iwsk->sk, rrf, len); if (ret < 0) return ret; } free(rrf); return 0; }