2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <alloca.h>
2N/A#include <sys/byteorder.h>
2N/A#include "fru_access_impl.h"
2N/A#include "fruraw.h"
2N/A
2N/A#pragma init(initialize_raw_access)
2N/A
2N/Astatic hash_obj_t *hash_table[TABLE_SIZE];
2N/Aextern raw_list_t *g_raw;
2N/A
2N/Astatic void
2N/Ainitialize_raw_access(void)
2N/A{
2N/A int count;
2N/A
2N/A for (count = 0; count < TABLE_SIZE; count++) {
2N/A hash_table[count] = NULL;
2N/A }
2N/A}
2N/A
2N/A
2N/Astatic hash_obj_t *
2N/Alookup_handle_object(handle_t handle, int object_type)
2N/A{
2N/A handle_t index_to_hash;
2N/A hash_obj_t *first_hash_obj;
2N/A hash_obj_t *next_hash_obj;
2N/A
2N/A index_to_hash = (handle % TABLE_SIZE);
2N/A
2N/A first_hash_obj = hash_table[index_to_hash];
2N/A for (next_hash_obj = first_hash_obj; next_hash_obj != NULL;
2N/A next_hash_obj = next_hash_obj->next) {
2N/A if ((handle == next_hash_obj->obj_hdl) &&
2N/A (object_type == next_hash_obj->object_type)) {
2N/A return (next_hash_obj);
2N/A }
2N/A }
2N/A return (NULL);
2N/A}
2N/A
2N/A
2N/Astatic void
2N/Aadd_hashobject_to_hashtable(hash_obj_t *hash_obj)
2N/A{
2N/A handle_t index_to_hash;
2N/A static uint64_t handle_count = 0;
2N/A
2N/A hash_obj->obj_hdl = ++handle_count; /* store the handle */
2N/A
2N/A /* where to add ? */
2N/A index_to_hash = ((hash_obj->obj_hdl) % TABLE_SIZE);
2N/A
2N/A hash_obj->next = hash_table[index_to_hash];
2N/A hash_table[index_to_hash] = hash_obj; /* hash obj. added */
2N/A
2N/A if (hash_obj->next != NULL) {
2N/A hash_obj->next->prev = hash_obj;
2N/A }
2N/A}
2N/A
2N/A
2N/Astatic hash_obj_t *
2N/Acreate_container_hash_object(void)
2N/A{
2N/A hash_obj_t *hash_obj;
2N/A container_obj_t *cont_obj;
2N/A
2N/A cont_obj = malloc(sizeof (container_obj_t));
2N/A if (cont_obj == NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A hash_obj = malloc(sizeof (hash_obj_t));
2N/A if (hash_obj == NULL) {
2N/A free(cont_obj);
2N/A return (NULL);
2N/A }
2N/A
2N/A cont_obj->sec_obj_list = NULL;
2N/A
2N/A hash_obj->object_type = CONTAINER_TYPE;
2N/A hash_obj->u.cont_obj = cont_obj;
2N/A hash_obj->next = NULL;
2N/A hash_obj->prev = NULL;
2N/A
2N/A return (hash_obj);
2N/A}
2N/A
2N/A
2N/Astatic hash_obj_t *
2N/Acreate_section_hash_object(void)
2N/A{
2N/A hash_obj_t *hash_obj;
2N/A section_obj_t *sec_obj;
2N/A
2N/A sec_obj = malloc(sizeof (section_obj_t));
2N/A if (sec_obj == NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A hash_obj = malloc(sizeof (hash_obj_t));
2N/A if (hash_obj == NULL) {
2N/A free(sec_obj);
2N/A return (NULL);
2N/A }
2N/A
2N/A sec_obj->next = NULL;
2N/A sec_obj->seg_obj_list = NULL;
2N/A
2N/A hash_obj->u.sec_obj = sec_obj;
2N/A hash_obj->object_type = SECTION_TYPE;
2N/A hash_obj->next = NULL;
2N/A hash_obj->prev = NULL;
2N/A
2N/A return (hash_obj);
2N/A}
2N/A
2N/A
2N/Astatic hash_obj_t *
2N/Acreate_segment_hash_object(void)
2N/A{
2N/A hash_obj_t *hash_obj;
2N/A segment_obj_t *seg_obj;
2N/A
2N/A seg_obj = malloc(sizeof (segment_obj_t));
2N/A if (seg_obj == NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A hash_obj = malloc(sizeof (hash_obj_t));
2N/A if (hash_obj == NULL) {
2N/A free(seg_obj);
2N/A return (NULL);
2N/A }
2N/A
2N/A seg_obj->next = NULL;
2N/A seg_obj->pkt_obj_list = NULL;
2N/A
2N/A hash_obj->object_type = SEGMENT_TYPE;
2N/A hash_obj->u.seg_obj = seg_obj;
2N/A hash_obj->next = NULL;
2N/A hash_obj->prev = NULL;
2N/A
2N/A return (hash_obj);
2N/A}
2N/A
2N/A
2N/Astatic hash_obj_t *
2N/Acreate_packet_hash_object(void)
2N/A{
2N/A hash_obj_t *hash_obj;
2N/A packet_obj_t *pkt_obj;
2N/A
2N/A pkt_obj = malloc(sizeof (packet_obj_t));
2N/A if (pkt_obj == NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A hash_obj = malloc(sizeof (hash_obj_t));
2N/A if (hash_obj == NULL) {
2N/A free(pkt_obj);
2N/A return (NULL);
2N/A }
2N/A
2N/A pkt_obj->next = NULL;
2N/A
2N/A hash_obj->object_type = PACKET_TYPE;
2N/A hash_obj->u.pkt_obj = pkt_obj;
2N/A hash_obj->next = NULL;
2N/A hash_obj->prev = NULL;
2N/A
2N/A return (hash_obj);
2N/A}
2N/A
2N/A
2N/A
2N/Astatic hash_obj_t *
2N/Aget_container_hash_object(int object_type, handle_t handle)
2N/A{
2N/A hash_obj_t *hash_obj;
2N/A
2N/A switch (object_type) {
2N/A case CONTAINER_TYPE:
2N/A break;
2N/A case SECTION_TYPE:
2N/A hash_obj = lookup_handle_object(handle, CONTAINER_TYPE);
2N/A if (hash_obj == NULL) {
2N/A return (NULL);
2N/A }
2N/A break;
2N/A case SEGMENT_TYPE:
2N/A hash_obj = lookup_handle_object(handle, SECTION_TYPE);
2N/A if (hash_obj == NULL) {
2N/A return (NULL);
2N/A }
2N/A hash_obj = lookup_handle_object(hash_obj->u.sec_obj->cont_hdl,
2N/A CONTAINER_TYPE);
2N/A break;
2N/A case PACKET_TYPE:
2N/A break;
2N/A default:
2N/A return (NULL);
2N/A }
2N/A
2N/A return (hash_obj);
2N/A}
2N/A
2N/A
2N/Astatic void
2N/Aadd_to_pkt_object_list(hash_obj_t *parent_obj, hash_obj_t *child_obj)
2N/A{
2N/A hash_obj_t *next_hash;
2N/A
2N/A /* add the packet object in the end of list */
2N/A child_obj->u.pkt_obj->segment_hdl = parent_obj->obj_hdl;
2N/A
2N/A if (parent_obj->u.seg_obj->pkt_obj_list == NULL) {
2N/A parent_obj->u.seg_obj->pkt_obj_list = child_obj;
2N/A return;
2N/A }
2N/A
2N/A for (next_hash = parent_obj->u.seg_obj->pkt_obj_list;
2N/A next_hash->u.pkt_obj->next != NULL;
2N/A next_hash = next_hash->u.pkt_obj->next) {
2N/A ;
2N/A }
2N/A
2N/A next_hash->u.pkt_obj->next = child_obj;
2N/A}
2N/A
2N/A
2N/Astatic void
2N/Afree_pkt_object_list(hash_obj_t *hash_obj)
2N/A{
2N/A hash_obj_t *next_obj;
2N/A hash_obj_t *free_obj;
2N/A
2N/A next_obj = hash_obj->u.seg_obj->pkt_obj_list;
2N/A while (next_obj != NULL) {
2N/A free_obj = next_obj;
2N/A next_obj = next_obj->u.pkt_obj->next;
2N/A /* if prev is NULL it's the first object in the list */
2N/A if (free_obj->prev == NULL) {
2N/A hash_table[(free_obj->obj_hdl % TABLE_SIZE)] =
2N/A free_obj->next;
2N/A if (free_obj->next != NULL) {
2N/A free_obj->next->prev = free_obj->prev;
2N/A }
2N/A } else {
2N/A free_obj->prev->next = free_obj->next;
2N/A if (free_obj->next != NULL) {
2N/A free_obj->next->prev = free_obj->prev;
2N/A }
2N/A }
2N/A
2N/A free(free_obj->u.pkt_obj->payload);
2N/A free(free_obj->u.pkt_obj);
2N/A free(free_obj);
2N/A }
2N/A
2N/A hash_obj->u.seg_obj->pkt_obj_list = NULL;
2N/A}
2N/A
2N/A
2N/Astatic void
2N/Afree_segment_hash(handle_t handle, hash_obj_t *sec_hash)
2N/A{
2N/A hash_obj_t *seg_hash;
2N/A hash_obj_t *next_hash;
2N/A
2N/A seg_hash = sec_hash->u.sec_obj->seg_obj_list;
2N/A if (seg_hash == NULL) {
2N/A return;
2N/A }
2N/A
2N/A if (seg_hash->obj_hdl == handle) {
2N/A sec_hash->u.sec_obj->seg_obj_list = seg_hash->u.seg_obj->next;
2N/A } else {
2N/A while (seg_hash->obj_hdl != handle) {
2N/A next_hash = seg_hash;
2N/A seg_hash = seg_hash->u.seg_obj->next;
2N/A if (seg_hash == NULL) {
2N/A return;
2N/A }
2N/A }
2N/A next_hash->u.seg_obj->next = seg_hash->u.seg_obj->next;
2N/A }
2N/A
2N/A if (seg_hash->prev == NULL) {
2N/A hash_table[(seg_hash->obj_hdl % TABLE_SIZE)] = seg_hash->next;
2N/A if (seg_hash->next != NULL) {
2N/A seg_hash->next->prev = NULL;
2N/A }
2N/A } else {
2N/A seg_hash->prev->next = seg_hash->next;
2N/A if (seg_hash->next != NULL) {
2N/A seg_hash->next->prev = seg_hash->prev;
2N/A }
2N/A }
2N/A
2N/A free_pkt_object_list(seg_hash);
2N/A free(seg_hash->u.seg_obj);
2N/A free(seg_hash);
2N/A}
2N/A
2N/A
2N/A
2N/Astatic void
2N/Aadd_to_sec_object_list(hash_obj_t *parent_obj, hash_obj_t *child_obj)
2N/A{
2N/A hash_obj_t *next_hash;
2N/A
2N/A child_obj->u.sec_obj->cont_hdl = parent_obj->obj_hdl;
2N/A if (parent_obj->u.cont_obj->sec_obj_list == NULL) {
2N/A parent_obj->u.cont_obj->sec_obj_list = child_obj;
2N/A return;
2N/A }
2N/A
2N/A for (next_hash = parent_obj->u.cont_obj->sec_obj_list;
2N/A next_hash->u.sec_obj->next != NULL;
2N/A next_hash = next_hash->u.sec_obj->next) {
2N/A ;
2N/A }
2N/A
2N/A next_hash->u.sec_obj->next = child_obj;
2N/A}
2N/A
2N/A
2N/Astatic void
2N/Aadd_to_seg_object_list(hash_obj_t *parent_obj, hash_obj_t *child_obj)
2N/A{
2N/A hash_obj_t *next_hash;
2N/A
2N/A child_obj->u.seg_obj->section_hdl = parent_obj->obj_hdl;
2N/A if (parent_obj->u.sec_obj->seg_obj_list == NULL) {
2N/A parent_obj->u.sec_obj->seg_obj_list = child_obj;
2N/A return;
2N/A }
2N/A
2N/A for (next_hash = parent_obj->u.sec_obj->seg_obj_list;
2N/A next_hash->u.seg_obj->next != NULL;
2N/A next_hash = next_hash->u.seg_obj->next) {
2N/A ;
2N/A }
2N/A
2N/A next_hash->u.seg_obj->next = child_obj;
2N/A}
2N/A
2N/A
2N/Astatic char *
2N/Atokenizer(char *buf, char *separator, char **nextBuf, char *matched)
2N/A{
2N/A int i = 0;
2N/A int j = 0;
2N/A
2N/A for (i = 0; buf[i] != '\0'; i++) {
2N/A for (j = 0; j < strlen(separator); j++) {
2N/A if (buf[i] == separator[j]) {
2N/A buf[i] = '\0';
2N/A *nextBuf = &(buf[i+1]);
2N/A *matched = separator[j];
2N/A return (buf);
2N/A }
2N/A }
2N/A }
2N/A
2N/A *nextBuf = buf;
2N/A *matched = '\0';
2N/A return (NULL);
2N/A}
2N/A
2N/A
2N/Astatic void
2N/Acopy_segment_layout(segment_t *seghdr, void *layout)
2N/A{
2N/A segment_layout_t *seg_layout;
2N/A
2N/A seg_layout = (segment_layout_t *)layout;
2N/A (void) memcpy(seghdr->name, &seg_layout->name, SEG_NAME_LEN);
2N/A seghdr->descriptor = GET_SEGMENT_DESCRIPTOR;
2N/A seghdr->offset = BE_16(seg_layout->offset);
2N/A seghdr->length = BE_16(seg_layout->length);
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Aget_container_info(const char *def_file, const char *cont_desc_str,
2N/A container_info_t *cont_info)
2N/A{
2N/A char *item;
2N/A char *token;
2N/A char *field;
2N/A char matched;
2N/A char buf[1024];
2N/A int foundIt = 0;
2N/A FILE *file = fopen(def_file, "r");
2N/A
2N/A if (file == NULL)
2N/A return (-1);
2N/A
2N/A cont_info->num_sections = 0;
2N/A
2N/A while (fgets(buf, sizeof (buf), file) != NULL) {
2N/A /* ignore all comments */
2N/A token = tokenizer(buf, "#", &field, &matched);
2N/A /* find the names */
2N/A token = tokenizer(buf, ":", &field, &matched);
2N/A if (token != 0x00) {
2N/A token = tokenizer(token, "|", &item, &matched);
2N/A while (token != 0x00) {
2N/A if (strcmp(token, cont_desc_str) == 0) {
2N/A foundIt = 1;
2N/A goto found;
2N/A }
2N/A token = tokenizer(item, "|", &item, &matched);
2N/A }
2N/A /* check the last remaining item */
2N/A if ((item != 0x00) &&
2N/A (strcmp(item, cont_desc_str) == 0)) {
2N/A foundIt = 1;
2N/A goto found;
2N/A }
2N/A }
2N/A }
2N/A
2N/Afound :
2N/A if (foundIt == 1) {
2N/A token = tokenizer(field, ":", &field, &matched);
2N/A if (token == 0x00) {
2N/A (void) fclose(file);
2N/A return (-1);
2N/A }
2N/A cont_info->header_ver = (headerrev_t)atoi(token);
2N/A
2N/A token = tokenizer(field, ":\n", &field, &matched);
2N/A while (token != 0x00) {
2N/A token = tokenizer(token, ",", &item, &matched);
2N/A if (token == 0x00) {
2N/A (void) fclose(file);
2N/A return (-1);
2N/A }
2N/A if (atoi(token) == 1) {
2N/A cont_info->section_info[cont_info->
2N/A num_sections].description.field.read_only
2N/A = 1;
2N/A } else if (atoi(token) == 0) {
2N/A cont_info->section_info[cont_info->
2N/A num_sections].description.field.read_only
2N/A = 0;
2N/A } else {
2N/A (void) fclose(file);
2N/A return (-1);
2N/A }
2N/A
2N/A token = tokenizer(item, ",", &item, &matched);
2N/A if (token == 0x00) {
2N/A (void) fclose(file);
2N/A return (-1);
2N/A }
2N/A
2N/A cont_info->section_info[cont_info->
2N/A num_sections].address = atoi(token);
2N/A if (item == '\0') {
2N/A (void) fclose(file);
2N/A return (-1);
2N/A }
2N/A cont_info->section_info[cont_info->num_sections].size =
2N/A atoi(item);
2N/A (cont_info->num_sections)++;
2N/A
2N/A token = tokenizer(field, ":\n ", &field, &matched);
2N/A }
2N/A }
2N/A (void) fclose(file);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A
2N/A/* ARGSUSED */
2N/Aint
2N/Afru_get_segments(section_hdl_t section, segment_t *segment, int maxseg,
2N/A door_cred_t *cred)
2N/A{
2N/A int count;
2N/A hash_obj_t *sec_object;
2N/A hash_obj_t *seg_object;
2N/A section_obj_t *sec_obj;
2N/A
2N/A sec_object = lookup_handle_object(section, SECTION_TYPE);
2N/A if (sec_object == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A sec_obj = sec_object->u.sec_obj;
2N/A if (sec_obj == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A if (sec_obj->num_of_segment > maxseg) {
2N/A return (-1);
2N/A }
2N/A
2N/A seg_object = sec_object->u.sec_obj->seg_obj_list;
2N/A if (seg_object == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A for (count = 0; count < sec_obj->num_of_segment; count++) {
2N/A
2N/A /* populate segment_t */
2N/A segment->handle = seg_object->obj_hdl;
2N/A (void) memcpy(segment->name,
2N/A seg_object->u.seg_obj->segment.name, SEG_NAME_LEN);
2N/A segment->descriptor = seg_object->u.seg_obj->segment.descriptor;
2N/A
2N/A segment->offset = seg_object->u.seg_obj->segment.offset;
2N/A segment->length = seg_object->u.seg_obj->segment.length;
2N/A seg_object = seg_object->u.seg_obj->next;
2N/A segment++;
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Araw_memcpy(void *buffer, raw_list_t *rawlist, int offset, int size)
2N/A{
2N/A if (offset + size > rawlist->size) {
2N/A size = rawlist->size - offset;
2N/A }
2N/A
2N/A (void) memcpy(buffer, &rawlist->raw[offset], size);
2N/A
2N/A return (size);
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Averify_header_crc8(headerrev_t head_ver, unsigned char *bytes, int length)
2N/A{
2N/A int crc_offset = 0;
2N/A unsigned char orig_crc8 = 0;
2N/A unsigned char calc_crc8 = 0;
2N/A
2N/A switch (head_ver) {
2N/A case SECTION_HDR_VER:
2N/A crc_offset = 4;
2N/A break;
2N/A default:
2N/A errno = EINVAL;
2N/A return (0);
2N/A }
2N/A
2N/A orig_crc8 = bytes[crc_offset];
2N/A bytes[crc_offset] = 0x00; /* clear for calc */
2N/A calc_crc8 = compute_crc8(bytes, length);
2N/A bytes[crc_offset] = orig_crc8; /* restore */
2N/A
2N/A return (orig_crc8 == calc_crc8);
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Aget_section(raw_list_t *rawlist, hash_obj_t *sec_hash, section_t *section)
2N/A{
2N/A int retval;
2N/A int size;
2N/A int count;
2N/A uint16_t hdrver;
2N/A hash_obj_t *seg_hash;
2N/A unsigned char *buffer;
2N/A section_obj_t *sec_obj;
2N/A section_layout_t sec_hdr;
2N/A segment_layout_t *seg_hdr;
2N/A segment_layout_t *seg_buf;
2N/A
2N/A sec_obj = sec_hash->u.sec_obj;
2N/A if (sec_obj == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A /* populate section_t */
2N/A section->handle = sec_hash->obj_hdl;
2N/A section->offset = sec_obj->section.offset;
2N/A section->length = sec_obj->section.length;
2N/A section->protection = sec_obj->section.protection;
2N/A section->version = sec_obj->section.version;
2N/A
2N/A /* read section header layout */
2N/A retval = raw_memcpy(&sec_hdr, rawlist, sec_obj->section.offset,
2N/A sizeof (sec_hdr));
2N/A
2N/A if (retval != sizeof (sec_hdr)) {
2N/A return (-1);
2N/A }
2N/A
2N/A
2N/A hdrver = GET_SECTION_HDR_VERSION;
2N/A
2N/A if ((sec_hdr.headertag != SECTION_HDR_TAG) &&
2N/A (hdrver != section->version)) {
2N/A return (-1);
2N/A }
2N/A
2N/A /* size = section layout + total sizeof segment header */
2N/A size = sizeof (sec_hdr) + ((sec_hdr.segmentcount)
2N/A * sizeof (segment_layout_t));
2N/A
2N/A buffer = alloca(size);
2N/A if (buffer == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A /* segment header buffer */
2N/A seg_buf = alloca(size - sizeof (sec_hdr));
2N/A if (seg_buf == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A /* read segment header */
2N/A retval = raw_memcpy(seg_buf, rawlist,
2N/A sec_obj->section.offset + sizeof (sec_hdr),
2N/A size - sizeof (sec_hdr));
2N/A
2N/A if (retval != (size - sizeof (sec_hdr))) {
2N/A return (-1);
2N/A }
2N/A
2N/A /* copy section header layout */
2N/A (void) memcpy(buffer, &sec_hdr, sizeof (sec_hdr));
2N/A
2N/A /* copy segment header layout */
2N/A (void) memcpy(buffer + sizeof (sec_hdr), seg_buf, size -
2N/A sizeof (sec_hdr));
2N/A
2N/A /* verify crc8 */
2N/A retval = verify_header_crc8(hdrver, buffer, size);
2N/A if (retval != TRUE) {
2N/A return (-1);
2N/A }
2N/A
2N/A section->version = hdrver;
2N/A sec_obj->section.version = hdrver;
2N/A
2N/A seg_hdr = (segment_layout_t *)seg_buf;
2N/A
2N/A /* bug fix for frutool */
2N/A if (sec_hash->u.sec_obj->seg_obj_list != NULL) {
2N/A return (0);
2N/A } else {
2N/A sec_obj->num_of_segment = 0;
2N/A }
2N/A for (count = 0; count < sec_hdr.segmentcount; count++, seg_hdr++) {
2N/A
2N/A seg_hash = create_segment_hash_object();
2N/A if (seg_hash == NULL) {
2N/A return (-1);
2N/A }
2N/A add_hashobject_to_hashtable(seg_hash);
2N/A copy_segment_layout(&seg_hash->u.seg_obj->segment, seg_hdr);
2N/A add_to_seg_object_list(sec_hash, seg_hash);
2N/A sec_obj->num_of_segment++;
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A/* ARGSUSED */
2N/Aint
2N/Afru_get_sections(container_hdl_t container, section_t *section, int maxsec,
2N/A door_cred_t *cred)
2N/A{
2N/A int count;
2N/A int num_sec = 0;
2N/A hash_obj_t *cont_object;
2N/A hash_obj_t *sec_hash;
2N/A
2N/A cont_object = lookup_handle_object(container, CONTAINER_TYPE);
2N/A if (cont_object == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A if (cont_object->u.cont_obj->num_of_section > maxsec) {
2N/A return (-1);
2N/A }
2N/A
2N/A sec_hash = cont_object->u.cont_obj->sec_obj_list;
2N/A if (sec_hash == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A for (count = 0; count < cont_object->u.cont_obj->num_of_section;
2N/A count++) {
2N/A section->version = -1;
2N/A /* populate section_t */
2N/A if (get_section(g_raw, sec_hash, section) == 0) {
2N/A section++;
2N/A num_sec++;
2N/A }
2N/A sec_hash = sec_hash->u.sec_obj->next;
2N/A }
2N/A return (num_sec);
2N/A}
2N/A
2N/A
2N/Astatic uint32_t
2N/Aget_checksum_crc(hash_obj_t *seg_hash, int data_size)
2N/A{
2N/A int protection;
2N/A int offset = 0;
2N/A uint32_t crc;
2N/A hash_obj_t *sec_hash;
2N/A hash_obj_t *pkt_hash;
2N/A unsigned char *buffer;
2N/A
2N/A sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
2N/A SECTION_TYPE);
2N/A if (sec_hash == NULL) {
2N/A return ((uint32_t)-1);
2N/A }
2N/A
2N/A buffer = alloca(data_size);
2N/A if (buffer == NULL) {
2N/A return ((uint32_t)-1);
2N/A }
2N/A
2N/A /* traverse the packet object list for all the tags and payload */
2N/A for (pkt_hash = seg_hash->u.seg_obj->pkt_obj_list; pkt_hash != NULL;
2N/A pkt_hash = pkt_hash->u.pkt_obj->next) {
2N/A (void) memcpy(buffer + offset, &pkt_hash->u.pkt_obj->tag,
2N/A pkt_hash->u.pkt_obj->tag_size);
2N/A offset += pkt_hash->u.pkt_obj->tag_size;
2N/A (void) memcpy(buffer + offset, pkt_hash->u.pkt_obj->payload,
2N/A pkt_hash->u.pkt_obj->paylen);
2N/A offset += pkt_hash->u.pkt_obj->paylen;
2N/A }
2N/A
2N/A protection = sec_hash->u.sec_obj->section.protection;
2N/A
2N/A if (protection == READ_ONLY_SECTION) { /* read-only section */
2N/A crc = compute_crc32(buffer, data_size);
2N/A } else { /* read/write section */
2N/A crc = compute_checksum32(buffer, data_size);
2N/A }
2N/A return (crc); /* computed crc */
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Aget_packet(raw_list_t *rawlist, void *buffer, int size, int offset)
2N/A{
2N/A int retval;
2N/A
2N/A retval = raw_memcpy(buffer, rawlist, offset, size);
2N/A
2N/A if (retval != -1) {
2N/A return (0);
2N/A }
2N/A return (-1);
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Aget_packets(hash_obj_t *seg_hash, raw_list_t *rawlist, int offset, int length)
2N/A{
2N/A int tag_size;
2N/A int paylen;
2N/A int retval;
2N/A int seg_limit = 0;
2N/A int pktcnt = 0;
2N/A char *data;
2N/A uint32_t crc;
2N/A uint32_t origcrc;
2N/A fru_tag_t tag;
2N/A hash_obj_t *pkt_hash_obj;
2N/A hash_obj_t *sec_hash;
2N/A fru_segdesc_t *segdesc;
2N/A fru_tagtype_t tagtype;
2N/A char *ignore_flag;
2N/A
2N/A retval = get_packet(rawlist, &tag, sizeof (fru_tag_t), offset);
2N/A if (retval == -1) {
2N/A return (-1);
2N/A }
2N/A
2N/A /* section hash object */
2N/A sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
2N/A SECTION_TYPE);
2N/A
2N/A if (sec_hash == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A seg_hash->u.seg_obj->trailer_offset = offset;
2N/A
2N/A data = (char *)&tag;
2N/A while (data[0] != SEG_TRAILER_TAG) {
2N/A tagtype = get_tag_type(&tag); /* verify tag type */
2N/A if (tagtype == -1) {
2N/A return (-1);
2N/A }
2N/A
2N/A tag_size = get_tag_size(tagtype);
2N/A if (tag_size == -1) {
2N/A return (-1);
2N/A }
2N/A
2N/A seg_limit += tag_size;
2N/A if (seg_limit > length) {
2N/A return (-1);
2N/A }
2N/A
2N/A paylen = get_payload_length((void *)&tag);
2N/A if (paylen == -1) {
2N/A return (-1);
2N/A }
2N/A
2N/A seg_limit += paylen;
2N/A if (seg_limit > length) {
2N/A return (-1);
2N/A }
2N/A if ((offset + tag_size + paylen) >
2N/A (sec_hash->u.sec_obj->section.offset +
2N/A sec_hash->u.sec_obj->section.length)) {
2N/A return (-1);
2N/A }
2N/A
2N/A pkt_hash_obj = create_packet_hash_object();
2N/A if (pkt_hash_obj == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A pkt_hash_obj->u.pkt_obj->payload = malloc(paylen);
2N/A if (pkt_hash_obj->u.pkt_obj->payload == NULL) {
2N/A free(pkt_hash_obj);
2N/A return (-1);
2N/A }
2N/A
2N/A offset += tag_size;
2N/A
2N/A retval = raw_memcpy(pkt_hash_obj->u.pkt_obj->payload, rawlist,
2N/A offset, paylen);
2N/A
2N/A if (retval != paylen) {
2N/A free(pkt_hash_obj->u.pkt_obj->payload);
2N/A free(pkt_hash_obj);
2N/A return (-1);
2N/A }
2N/A
2N/A /* don't change this */
2N/A pkt_hash_obj->u.pkt_obj->tag.raw_data = 0;
2N/A (void) memcpy(&pkt_hash_obj->u.pkt_obj->tag, &tag, tag_size);
2N/A pkt_hash_obj->u.pkt_obj->paylen = paylen;
2N/A pkt_hash_obj->u.pkt_obj->tag_size = tag_size;
2N/A pkt_hash_obj->u.pkt_obj->payload_offset = offset;
2N/A
2N/A offset += paylen;
2N/A
2N/A add_hashobject_to_hashtable(pkt_hash_obj);
2N/A add_to_pkt_object_list(seg_hash, pkt_hash_obj);
2N/A
2N/A pktcnt++;
2N/A
2N/A retval = get_packet(rawlist, &tag, sizeof (fru_tag_t),
2N/A offset);
2N/A if (retval == -1) {
2N/A return (retval);
2N/A }
2N/A
2N/A data = (char *)&tag;
2N/A }
2N/A
2N/A segdesc = (fru_segdesc_t *)&seg_hash->u.seg_obj->segment.descriptor;
2N/A
2N/A seg_hash->u.seg_obj->trailer_offset = offset;
2N/A
2N/A if (!segdesc->field.ignore_checksum) {
2N/A crc = get_checksum_crc(seg_hash, seg_limit);
2N/A offset = seg_hash->u.seg_obj->segment.offset;
2N/A
2N/A retval = raw_memcpy(&origcrc, rawlist, offset + seg_limit + 1,
2N/A sizeof (origcrc));
2N/A
2N/A ignore_flag = getenv(IGNORE_CHECK);
2N/A if (ignore_flag != NULL) {
2N/A return (pktcnt);
2N/A }
2N/A
2N/A if (retval != sizeof (origcrc)) {
2N/A return (-1);
2N/A }
2N/A
2N/A origcrc = BE_32(origcrc);
2N/A if (origcrc != crc) {
2N/A seg_hash->u.seg_obj->trailer_offset = offset;
2N/A return (-1);
2N/A }
2N/A }
2N/A
2N/A return (pktcnt);
2N/A}
2N/A
2N/A/* ARGSUSED */
2N/Aint
2N/Afru_get_num_sections(container_hdl_t container, door_cred_t *cred)
2N/A{
2N/A hash_obj_t *hash_object;
2N/A
2N/A hash_object = lookup_handle_object(container, CONTAINER_TYPE);
2N/A if (hash_object == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A return (hash_object->u.cont_obj->num_of_section);
2N/A}
2N/A
2N/A/* ARGSUSED */
2N/Aint
2N/Afru_get_num_segments(section_hdl_t section, door_cred_t *cred)
2N/A{
2N/A hash_obj_t *sec_object;
2N/A section_obj_t *sec_obj;
2N/A
2N/A sec_object = lookup_handle_object(section, SECTION_TYPE);
2N/A if (sec_object == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A sec_obj = sec_object->u.sec_obj;
2N/A if (sec_obj == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A return (sec_obj->num_of_segment);
2N/A}
2N/A
2N/A/* ARGSUSED */
2N/Aint
2N/Afru_get_num_packets(segment_hdl_t segment, door_cred_t *cred)
2N/A{
2N/A int pktcnt;
2N/A int length;
2N/A uint16_t offset;
2N/A hash_obj_t *cont_hash_obj;
2N/A hash_obj_t *seg_hash;
2N/A hash_obj_t *sec_hash;
2N/A fru_segdesc_t *segdesc;
2N/A segment_obj_t *segment_object;
2N/A
2N/A seg_hash = lookup_handle_object(segment, SEGMENT_TYPE);
2N/A if (seg_hash == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A segment_object = seg_hash->u.seg_obj;
2N/A if (segment_object == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A segdesc = (fru_segdesc_t *)&segment_object->segment.descriptor;
2N/A if (segdesc->field.opaque) {
2N/A return (0);
2N/A }
2N/A
2N/A offset = segment_object->segment.offset;
2N/A length = segment_object->segment.length;
2N/A
2N/A cont_hash_obj = get_container_hash_object(SEGMENT_TYPE,
2N/A segment_object->section_hdl);
2N/A
2N/A if (cont_hash_obj == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A if (seg_hash->u.seg_obj->pkt_obj_list != NULL) {
2N/A return (segment_object->num_of_packets);
2N/A }
2N/A /* section hash object */
2N/A sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl,
2N/A SECTION_TYPE);
2N/A if (sec_hash == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A /* valid segment header b'cos crc8 already validated */
2N/A if (offset < sec_hash->u.sec_obj->section.offset) {
2N/A return (-1);
2N/A }
2N/A
2N/A segment_object->num_of_packets = 0;
2N/A
2N/A pktcnt = get_packets(seg_hash, g_raw, offset, length);
2N/A if (pktcnt == -1) {
2N/A free_pkt_object_list(seg_hash);
2N/A seg_hash->u.seg_obj->pkt_obj_list = NULL;
2N/A }
2N/A
2N/A segment_object->num_of_packets = pktcnt;
2N/A
2N/A return (segment_object->num_of_packets);
2N/A}
2N/A
2N/A/* ARGSUSED */
2N/Aint
2N/Afru_get_packets(segment_hdl_t segment, packet_t *packet, int maxpackets,
2N/A door_cred_t *cred)
2N/A{
2N/A int count;
2N/A hash_obj_t *seg_hash_obj;
2N/A hash_obj_t *pkt_hash_obj;
2N/A
2N/A /* segment hash object */
2N/A seg_hash_obj = lookup_handle_object(segment, SEGMENT_TYPE);
2N/A if (seg_hash_obj == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A if (seg_hash_obj->u.seg_obj->num_of_packets != maxpackets) {
2N/A return (-1);
2N/A }
2N/A
2N/A pkt_hash_obj = seg_hash_obj->u.seg_obj->pkt_obj_list;
2N/A if (pkt_hash_obj == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A for (count = 0; count < maxpackets; count++, packet++) {
2N/A packet->handle = pkt_hash_obj->obj_hdl;
2N/A packet->tag = 0;
2N/A (void) memcpy(&packet->tag, &pkt_hash_obj->u.pkt_obj->tag,
2N/A pkt_hash_obj->u.pkt_obj->tag_size);
2N/A pkt_hash_obj = pkt_hash_obj->u.pkt_obj->next;
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/* ARGSUSED */
2N/Assize_t
2N/Afru_get_payload(packet_hdl_t packet, void *buffer, size_t nbytes,
2N/A door_cred_t *cred)
2N/A{
2N/A hash_obj_t *packet_hash_obj;
2N/A
2N/A /* packet hash object */
2N/A packet_hash_obj = lookup_handle_object(packet, PACKET_TYPE);
2N/A if (packet_hash_obj == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A /* verify payload length */
2N/A if (nbytes != packet_hash_obj->u.pkt_obj->paylen) {
2N/A return (-1);
2N/A }
2N/A
2N/A (void) memcpy(buffer, packet_hash_obj->u.pkt_obj->payload, nbytes);
2N/A return (nbytes);
2N/A}
2N/A
2N/A
2N/Acontainer_hdl_t
2N/Aopen_raw_data(raw_list_t *node)
2N/A{
2N/A char *cont_conf_file = NULL;
2N/A hash_obj_t *cont_hash_obj;
2N/A hash_obj_t *sec_hash_obj;
2N/A container_info_t cont_info;
2N/A int retval;
2N/A int count;
2N/A
2N/A cont_hash_obj = create_container_hash_object();
2N/A if (cont_hash_obj == NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A add_hashobject_to_hashtable(cont_hash_obj);
2N/A
2N/A (void) strncpy(cont_hash_obj->u.cont_obj->device_pathname, "unknown",
2N/A sizeof (cont_hash_obj->u.cont_obj->device_pathname));
2N/A
2N/A cont_conf_file = getenv(FRU_CONT_CONF_ENV_VAR);
2N/A if (cont_conf_file == NULL) {
2N/A cont_conf_file = FRU_CONT_CONF_SPARC;
2N/A retval = get_container_info(cont_conf_file, node->cont_type,
2N/A &cont_info);
2N/A if (retval < 0) {
2N/A cont_conf_file = FRU_CONT_CONF_X86;
2N/A retval = get_container_info(cont_conf_file,
2N/A node->cont_type, &cont_info);
2N/A }
2N/A } else {
2N/A retval = get_container_info(cont_conf_file, node->cont_type,
2N/A &cont_info);
2N/A }
2N/A
2N/A if (retval < 0) {
2N/A return (NULL);
2N/A }
2N/A
2N/A cont_hash_obj->u.cont_obj->num_of_section = cont_info.num_sections;
2N/A cont_hash_obj->u.cont_obj->sec_obj_list = NULL;
2N/A
2N/A for (count = 0; count < cont_info.num_sections; count++) {
2N/A sec_hash_obj = create_section_hash_object();
2N/A if (sec_hash_obj == NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A add_hashobject_to_hashtable(sec_hash_obj);
2N/A
2N/A sec_hash_obj->u.sec_obj->section.offset =
2N/A cont_info.section_info[count].address;
2N/A
2N/A sec_hash_obj->u.sec_obj->section.protection =
2N/A cont_info.section_info[count].description.field.read_only;
2N/A
2N/A sec_hash_obj->u.sec_obj->section.length =
2N/A cont_info.section_info[count].size;
2N/A sec_hash_obj->u.sec_obj->section.version =
2N/A cont_info.header_ver;
2N/A
2N/A add_to_sec_object_list(cont_hash_obj, sec_hash_obj);
2N/A }
2N/A
2N/A return (cont_hash_obj->obj_hdl);
2N/A}
2N/A
2N/A
2N/Aint
2N/Afru_close_container(container_hdl_t container)
2N/A{
2N/A hash_obj_t *hash_obj;
2N/A hash_obj_t *prev_hash;
2N/A hash_obj_t *sec_hash_obj;
2N/A handle_t obj_hdl;
2N/A
2N/A /* lookup for container hash object */
2N/A hash_obj = lookup_handle_object(container, CONTAINER_TYPE);
2N/A if (hash_obj == NULL) {
2N/A return (0);
2N/A }
2N/A
2N/A /* points to section object list */
2N/A sec_hash_obj = hash_obj->u.cont_obj->sec_obj_list;
2N/A
2N/A /* traverse section object list */
2N/A while (sec_hash_obj != NULL) {
2N/A
2N/A /* traverse segment hash object in the section */
2N/A while (sec_hash_obj->u.sec_obj->seg_obj_list != NULL) {
2N/A /* object handle of the segment hash object */
2N/A obj_hdl =
2N/A sec_hash_obj->u.sec_obj->seg_obj_list->obj_hdl;
2N/A free_segment_hash(obj_hdl, sec_hash_obj);
2N/A }
2N/A
2N/A /* going to free section hash object, relink the hash object */
2N/A if (sec_hash_obj->prev == NULL) {
2N/A hash_table[(sec_hash_obj->obj_hdl % TABLE_SIZE)] =
2N/A sec_hash_obj->next;
2N/A if (sec_hash_obj->next != NULL) {
2N/A sec_hash_obj->next->prev = NULL;
2N/A }
2N/A } else {
2N/A sec_hash_obj->prev->next = sec_hash_obj->next;
2N/A if (sec_hash_obj->next != NULL) {
2N/A sec_hash_obj->next->prev = sec_hash_obj->prev;
2N/A }
2N/A }
2N/A
2N/A prev_hash = sec_hash_obj;
2N/A sec_hash_obj = sec_hash_obj->u.sec_obj->next;
2N/A
2N/A free(prev_hash->u.sec_obj); /* free section hash object */
2N/A free(prev_hash); /* free section hash */
2N/A }
2N/A
2N/A /* free container hash object */
2N/A if (hash_obj->prev == NULL) {
2N/A hash_table[(hash_obj->obj_hdl % TABLE_SIZE)] =
2N/A hash_obj->next;
2N/A if (hash_obj->next != NULL) {
2N/A hash_obj->next->prev = NULL;
2N/A }
2N/A } else {
2N/A hash_obj->prev->next = hash_obj->next;
2N/A if (hash_obj->next != NULL) {
2N/A hash_obj->next->prev = hash_obj->prev;
2N/A }
2N/A }
2N/A
2N/A free(hash_obj->u.cont_obj);
2N/A free(hash_obj);
2N/A
2N/A return (0);
2N/A}