/** * proto_lbs_request_parse(P, R): * Parse the packet ${P} into the LBS request structure ${R}. */ static int proto_lbs_request_parse(const struct wire_packet * P, struct proto_lbs_request * R) { /* Store request ID. */ R->ID = P->ID; /* Sanity-check packet length. */ if (P->len < 4) goto err0; /* Figure out request type. */ R->type = be32dec(&P->buf[0]); /* Parse packet. */ switch (R->type) { case PROTO_LBS_PARAMS: case PROTO_LBS_PARAMS2: if (P->len != 4) goto err0; /* Nothing to parse. */ break; case PROTO_LBS_GET: if (P->len != 12) goto err0; R->r.get.blkno = be64dec(&P->buf[4]); break; case PROTO_LBS_APPEND: if (P->len < 16) goto err0; R->r.append.nblks = be32dec(&P->buf[4]); R->r.append.blkno = be64dec(&P->buf[8]); if (R->r.append.nblks == 0) goto err0; if ((P->len - 16) % R->r.append.nblks) goto err0; R->r.append.blklen = (P->len - 16) / R->r.append.nblks; if ((R->r.append.buf = malloc(P->len - 16)) == NULL) goto err0; memcpy(R->r.append.buf, &P->buf[16], P->len - 16); break; case PROTO_LBS_FREE: if (P->len != 12) goto err0; R->r.free.blkno = be64dec(&P->buf[4]); break; default: goto err0; } /* Success! */ return (0); err0: /* Failure! */ return (-1); }
static int test_be64dec() { uint8_t buf1[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; uint8_t buf2[] = { 0xff, 0xef, 0xdf, 0xcf, 0xbf, 0xaf, 0x9f, 0xaf }; if(be64dec(buf1) != 0x0102030405060708ULL) return -1; if(be64dec(buf2) != 0xffefdfcfbfaf9fafULL) return -1; return 0; }
static uint64_t tdecode(int t, ...) { uint8_t buf[16]; bool be = t > 0; va_list ap; uint64_t val = 777; int i; if (t < 0) t = -t; va_start(ap, t); memset(buf, 0xC1, sizeof(buf)); for (i = 0; i < t; i++) buf[i] = va_arg(ap, int); va_end(ap); if (be) { switch (t) { case 2: val = be16dec(buf); break; case 4: val = be32dec(buf); break; case 8: val = be64dec(buf); break; } } else { switch (t) { case 2: val = le16dec(buf); break; case 4: val = le32dec(buf); break; case 8: val = le64dec(buf); break; } } return val; }
/* Callback for reading DeletedMarker from deleteto_init. */ static int callback_deletedmarker_get(void * cookie, int failed, size_t len, const uint8_t * buf) { struct deleteto * D = cookie; /* If we failed to read the file, die. */ if (failed) { warn0("Could not read DeletedMarker from S3"); goto err0; } /* We should have 8 bytes. */ if (len != 8) { warn0("DeletedMarker has incorrect size: %zu"); goto err0; } /* Parse the value. */ D->M = be64dec(buf); /* We're done. */ D->done = 1; /* Success! */ return (0); err0: /* Failure! */ return (-1); }
static void mpt_print_scsi_tmf_request(MSG_SCSI_TASK_MGMT *msg) { mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg); printf("\tLun 0x%jx\n", (uintmax_t)be64dec(msg->LUN)); printf("\tTaskType %s\n", mpt_scsi_tm_type(msg->TaskType)); printf("\tTaskMsgContext 0x%08x\n", msg->TaskMsgContext); }
void read_kdfp(uint8_t *addr, kdfp *kdfp) { uint64_t *N = (uint64_t *) (addr + SALT_LEN); uint32_t *r = (uint32_t *) (N + 1); uint32_t *p = (uint32_t *) (r + 1); memcpy(kdfp->salt, addr, SALT_LEN); kdfp->N = be64dec(N); kdfp->r = be32dec(r); kdfp->p = be32dec(p); }
static void mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *orig_msg) { MSG_SCSI_IO_REQUEST local, *msg = &local; int i; bcopy(orig_msg, msg, sizeof (MSG_SCSI_IO_REQUEST)); mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg); printf("\tBus: %d\n", msg->Bus); printf("\tTargetID %d\n", msg->TargetID); printf("\tSenseBufferLength %d\n", msg->SenseBufferLength); printf("\tLUN: 0x%jx\n", (uintmax_t)be64dec(msg->LUN)); printf("\tControl 0x%08x ", msg->Control); #define MPI_PRINT_FIELD(x) \ case MPI_SCSIIO_CONTROL_ ## x : \ printf(" " #x " "); \ break switch (msg->Control & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK) { MPI_PRINT_FIELD(NODATATRANSFER); MPI_PRINT_FIELD(WRITE); MPI_PRINT_FIELD(READ); default: printf(" Invalid DIR! "); break; } switch (msg->Control & MPI_SCSIIO_CONTROL_TASKATTRIBUTE_MASK) { MPI_PRINT_FIELD(SIMPLEQ); MPI_PRINT_FIELD(HEADOFQ); MPI_PRINT_FIELD(ORDEREDQ); MPI_PRINT_FIELD(ACAQ); MPI_PRINT_FIELD(UNTAGGED); MPI_PRINT_FIELD(NO_DISCONNECT); default: printf(" Unknown attribute! "); break; } printf("\n"); #undef MPI_PRINT_FIELD printf("\tDataLength\t0x%08x\n", msg->DataLength); printf("\tSenseBufAddr\t0x%08x\n", msg->SenseBufferLowAddr); printf("\tCDB[0:%d]\t", msg->CDBLength); for (i = 0; i < msg->CDBLength; i++) printf("%02x ", msg->CDB[i]); printf("\n"); if ((msg->Control & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK) != MPI_SCSIIO_CONTROL_NODATATRANSFER ) { mpt_dump_sgl(&orig_msg->SGL, ((char *)&orig_msg->SGL)-(char *)orig_msg); } }
static void mpt_print_scsi_target_status_send_request(MSG_TARGET_STATUS_SEND_REQUEST *msg) { SGE_IO_UNION x; mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg); printf("\tStatusCode 0x%02x\n", msg->StatusCode); printf("\tStatusFlags 0x%02x\n", msg->StatusFlags); printf("\tQueueTag 0x%04x\n", msg->QueueTag); printf("\tReplyWord 0x%08x\n", msg->ReplyWord); printf("\tLun 0x%jx\n", (uintmax_t)be64dec(msg->LUN)); x.u.Simple = msg->StatusDataSGE; mpt_dump_sgl(&x, 0); }
static void mpt_print_scsi_target_assist_request(PTR_MSG_TARGET_ASSIST_REQUEST msg) { mpt_print_request_hdr((MSG_REQUEST_HEADER *)msg); printf("\tStatusCode 0x%02x\n", msg->StatusCode); printf("\tTargetAssist 0x%02x\n", msg->TargetAssistFlags); printf("\tQueueTag 0x%04x\n", msg->QueueTag); printf("\tReplyWord 0x%08x\n", msg->ReplyWord); printf("\tLun 0x%jx\n", (uintmax_t)be64dec(msg->LUN)); printf("\tRelativeOff 0x%08x\n", msg->RelativeOffset); printf("\tDataLength 0x%08x\n", msg->DataLength); mpt_dump_sgl(msg->SGL, 0); }
static void le_lebuffer_copytodesc(struct lance_softc *sc, void *fromv, int off, int len) { struct le_lebuffer_softc *lesc = (struct le_lebuffer_softc *)sc; caddr_t from = fromv; for (; len >= 8; len -= 8, off += 8, from += 8) bus_write_8(lesc->sc_bres, off, be64dec(from)); for (; len >= 4; len -= 4, off += 4, from += 4) bus_write_4(lesc->sc_bres, off, be32dec(from)); for (; len >= 2; len -= 2, off += 2, from += 2) bus_write_2(lesc->sc_bres, off, be16dec(from)); if (len == 1) bus_write_1(lesc->sc_bres, off, *from); }
static int callback_register_response(void * cookie, NETPACKET_CONNECTION * NPC, int status, uint8_t packettype, const uint8_t * packetbuf, size_t packetlen) { struct register_internal * C = cookie; uint8_t hmac_actual[32]; (void)NPC; /* UNUSED */ (void)packetlen; /* UNUSED */ /* Handle errors. */ if (status != NETWORK_STATUS_OK) { netproto_printerr(status); goto err0; } /* Make sure we received the right type of packet. */ if (packettype != NETPACKET_REGISTER_RESPONSE) goto err1; /* Verify packet hmac. */ if ((packetbuf[0] == 0) || (packetbuf[0] == 3)) { crypto_hash_data_key_2(C->register_key, 32, &packettype, 1, packetbuf, 9, hmac_actual); } else { memset(hmac_actual, 0, 32); } if (crypto_verify_bytes(hmac_actual, &packetbuf[9], 32)) goto err1; /* Record status code and machine number returned by server. */ C->status = packetbuf[0]; C->machinenum = be64dec(&packetbuf[1]); /* We have received a response. */ C->done = 1; /* Success! */ return (0); err1: netproto_printerr(NETPROTO_STATUS_PROTERR); err0: /* Failure! */ return (-1); }
static u_int64_t elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t member) { u_int64_t val; val = 0; switch (e->e_ident[EI_CLASS]) { case ELFCLASS32: base = (char *)base + elf32_offsets[member]; switch (e->e_ident[EI_DATA]) { case ELFDATA2MSB: val = be32dec(base); break; case ELFDATA2LSB: val = le32dec(base); break; case ELFDATANONE: errx(1, "invalid data format"); } break; case ELFCLASS64: base = (char *)base + elf64_offsets[member]; switch (e->e_ident[EI_DATA]) { case ELFDATA2MSB: val = be64dec(base); break; case ELFDATA2LSB: val = le64dec(base); break; case ELFDATANONE: errx(1, "invalid data format"); } break; case ELFCLASSNONE: errx(1, "invalid class"); } return val; }
int cfcs_init(void) { struct cfcs_softc *softc; struct ccb_setasync csa; struct ctl_frontend *fe; #ifdef NEEDTOPORT char wwnn[8]; #endif int retval; /* Don't continue if CTL is disabled */ if (ctl_disable != 0) return (0); softc = &cfcs_softc; retval = 0; bzero(softc, sizeof(*softc)); sprintf(softc->lock_desc, "ctl2cam"); mtx_init(&softc->lock, softc->lock_desc, NULL, MTX_DEF); fe = &softc->fe; fe->port_type = CTL_PORT_INTERNAL; /* XXX KDM what should the real number be here? */ fe->num_requested_ctl_io = 4096; snprintf(softc->port_name, sizeof(softc->port_name), "ctl2cam"); fe->port_name = softc->port_name; fe->port_online = cfcs_online; fe->port_offline = cfcs_offline; fe->onoff_arg = softc; fe->targ_enable = cfcs_targ_enable; fe->targ_disable = cfcs_targ_disable; fe->lun_enable = cfcs_lun_enable; fe->lun_disable = cfcs_lun_disable; fe->targ_lun_arg = softc; fe->fe_datamove = cfcs_datamove; fe->fe_done = cfcs_done; /* XXX KDM what should we report here? */ /* XXX These should probably be fetched from CTL. */ fe->max_targets = 1; fe->max_target_id = 15; retval = ctl_frontend_register(fe, /*master_SC*/ 1); if (retval != 0) { printf("%s: ctl_frontend_register() failed with error %d!\n", __func__, retval); mtx_destroy(&softc->lock); return (1); } /* * Get the WWNN out of the database, and create a WWPN as well. */ #ifdef NEEDTOPORT ddb_GetWWNN((char *)wwnn); softc->wwnn = be64dec(wwnn); softc->wwpn = softc->wwnn + (softc->fe.targ_port & 0xff); #endif /* * If the CTL frontend didn't tell us what our WWNN/WWPN is, go * ahead and set something random. */ if (fe->wwnn == 0) { uint64_t random_bits; arc4rand(&random_bits, sizeof(random_bits), 0); softc->wwnn = (random_bits & 0x0000000fffffff00ULL) | /* Company ID */ 0x5000000000000000ULL | /* NL-Port */ 0x0300; softc->wwpn = softc->wwnn + fe->targ_port + 1; fe->wwnn = softc->wwnn; fe->wwpn = softc->wwpn; } else { softc->wwnn = fe->wwnn; softc->wwpn = fe->wwpn; } mtx_lock(&softc->lock); softc->devq = cam_simq_alloc(fe->num_requested_ctl_io); if (softc->devq == NULL) { printf("%s: error allocating devq\n", __func__); retval = ENOMEM; goto bailout; } softc->sim = cam_sim_alloc(cfcs_action, cfcs_poll, softc->port_name, softc, /*unit*/ 0, &softc->lock, 1, fe->num_requested_ctl_io, softc->devq); if (softc->sim == NULL) { printf("%s: error allocating SIM\n", __func__); retval = ENOMEM; goto bailout; } if (xpt_bus_register(softc->sim, NULL, 0) != CAM_SUCCESS) { printf("%s: error registering SIM\n", __func__); retval = ENOMEM; goto bailout; } if (xpt_create_path(&softc->path, /*periph*/NULL, cam_sim_path(softc->sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { printf("%s: error creating path\n", __func__); xpt_bus_deregister(cam_sim_path(softc->sim)); retval = 1; goto bailout; } xpt_setup_ccb(&csa.ccb_h, softc->path, CAM_PRIORITY_NONE); csa.ccb_h.func_code = XPT_SASYNC_CB; csa.event_enable = AC_LOST_DEVICE; csa.callback = cfcs_async; csa.callback_arg = softc->sim; xpt_action((union ccb *)&csa); mtx_unlock(&softc->lock); return (retval); bailout: if (softc->sim) cam_sim_free(softc->sim, /*free_devq*/ TRUE); else if (softc->devq) cam_simq_free(softc->devq); mtx_unlock(&softc->lock); mtx_destroy(&softc->lock); return (retval); }
void plist_from_bin(const char *plist_bin, uint32_t length, plist_t * plist) { char *trailer = NULL; uint8_t offset_size = 0; uint8_t dict_param_size = 0; uint64_t num_objects = 0; uint64_t root_object = 0; uint64_t offset_table_index = 0; plist_t *nodeslist = NULL; uint64_t i = 0; uint64_t current_offset = 0; char *offset_table = NULL; uint32_t j = 0, str_i = 0, str_j = 0; uint32_t index1 = 0, index2 = 0; //first check we have enough data if (!(length >= BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE + BPLIST_TRL_SIZE)) return; //check that plist_bin in actually a plist if (memcmp(plist_bin, BPLIST_MAGIC, BPLIST_MAGIC_SIZE) != 0) return; //check for known version if (memcmp(plist_bin + BPLIST_MAGIC_SIZE, BPLIST_VERSION, BPLIST_VERSION_SIZE) != 0) return; //now parse trailer trailer = (char *) (plist_bin + (length - BPLIST_TRL_SIZE)); offset_size = trailer[BPLIST_TRL_OFFSIZE_IDX]; dict_param_size = trailer[BPLIST_TRL_PARMSIZE_IDX]; num_objects = be64dec(trailer + BPLIST_TRL_NUMOBJ_IDX); root_object = be64dec(trailer + BPLIST_TRL_ROOTOBJ_IDX); offset_table_index = be64dec(trailer + BPLIST_TRL_OFFTAB_IDX); if (num_objects == 0) return; //allocate serialized array of nodes nodeslist = (plist_t *) malloc(sizeof(plist_t) * num_objects); if (!nodeslist) return; //parse serialized nodes offset_table = (char *) (plist_bin + offset_table_index); for (i = 0; i < num_objects; i++) { char *obj = NULL; current_offset = UINT_TO_HOST(offset_table + i * offset_size, offset_size); obj = (char *) (plist_bin + current_offset); nodeslist[i] = parse_bin_node(obj, dict_param_size, &obj); } //setup children for structured types for (i = 0; i < num_objects; i++) { plist_data_t data = plist_get_data(nodeslist[i]); switch (data->type) { case PLIST_DICT: for (j = 0; j < data->length; j++) { str_i = j * dict_param_size; str_j = (j + data->length) * dict_param_size; index1 = UINT_TO_HOST(data->buff + str_i, dict_param_size); index2 = UINT_TO_HOST(data->buff + str_j, dict_param_size); //first one is actually a key plist_get_data(nodeslist[index1])->type = PLIST_KEY; if (index1 < num_objects) { if (G_NODE_IS_ROOT(nodeslist[index1])) g_node_append(nodeslist[i], nodeslist[index1]); else g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL)); } if (index2 < num_objects) { if (G_NODE_IS_ROOT(nodeslist[index2])) g_node_append(nodeslist[i], nodeslist[index2]); else g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index2], copy_plist_data, NULL)); } } free(data->buff); break; case PLIST_ARRAY: for (j = 0; j < data->length; j++) { str_j = j * dict_param_size; index1 = UINT_TO_HOST(data->buff + str_j, dict_param_size); if (index1 < num_objects) { if (G_NODE_IS_ROOT(nodeslist[index1])) g_node_append(nodeslist[i], nodeslist[index1]); else g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL)); } } free(data->buff); break; default: break; } } *plist = nodeslist[root_object]; free(nodeslist); }
/** * storage_findfiles(path): * Look for files named "blks_<16 hex digits>" in the directory ${path}. * Return an elastic queue of struct storage_file, in order of increasing * fileno. */ struct elasticqueue * storage_findfiles(const char * path) { struct stat sb; DIR * dir; struct dirent * dp; struct ptrheap * H; struct storage_file * sf; char * s; uint8_t fileno_exp[8]; struct elasticqueue * Q; /* Create a heap for holding storage_file structures. */ if ((H = ptrheap_init(fs_compar, NULL, NULL)) == NULL) goto err0; /* Create a queue for holding the structures in sorted order. */ if ((Q = elasticqueue_init(sizeof(struct storage_file))) == NULL) goto err1; /* Open the storage directory. */ if ((dir = opendir(path)) == NULL) { warnp("Cannot open storage directory: %s", path); goto err2; } /* * Look for files named "blks_<64-bit hexified first block #>" and * create storage_file structures for each. */ while ((errno = 0), ((dp = readdir(dir)) != NULL)) { /* Skip anything which isn't the right length. */ if (strlen(dp->d_name) != strlen("blks_0123456789abcdef")) continue; /* Skip anything which doesn't start with "blks_". */ if (strncmp(dp->d_name, "blks_", 5)) continue; /* Make sure the name has 8 hexified bytes and parse. */ if (unhexify(&dp->d_name[5], fileno_exp, 8)) continue; /* Construct a full path to the file and stat. */ if (asprintf(&s, "%s/%s", path, dp->d_name) == -1) { warnp("asprintf"); goto err3; } if (lstat(s, &sb)) { warnp("stat(%s)", s); goto err4; } /* Skip anything other than regular files. */ if (!S_ISREG(sb.st_mode)) goto next; /* Allocate a file_state structure. */ if ((sf = malloc(sizeof(struct storage_file))) == NULL) goto err4; /* Fill in file number and size. */ sf->fileno = be64dec(fileno_exp); sf->len = sb.st_size; /* Insert the file into the heap. */ if (ptrheap_add(H, sf)) goto err5; next: /* Free the full path to the file. */ free(s); } if (errno != 0) { warnp("Error reading storage directory: %s", path); goto err3; } /* Close the storage directory. */ while (closedir(dir)) { /* Retry if we were interrupted. */ if (errno == EINTR) continue; /* Oops, something bad happened. */ warnp("Error closing storage directory: %s", path); goto err2; } /* Suck structures from the heap into the queue. */ while ((sf = ptrheap_getmin(H)) != NULL) { if (elasticqueue_add(Q, sf)) goto err2; free(sf); ptrheap_deletemin(H); } /* Free the (now empty) heap. */ ptrheap_free(H); /* Success! */ return (Q); err5: free(sf); err4: free(s); err3: closedir(dir); err2: elasticqueue_free(Q); err1: while ((sf = ptrheap_getmin(H)) != NULL) { ptrheap_deletemin(H); free(sf); } ptrheap_free(H); err0: /* Failure! */ return (NULL); }