/*-----------------------------------------------------------*/ UINT8_T WriteBlockLink(UINT8_T index, BlockLink *bl) { UINT16_T crc; UINT8_T *buf; UINT8_T i; buf=(UINT8_T*)local_malloc(sl->sector[index].bl_size); if(buf==NULL) return ERR_LOCAL_MALLOC; memset((void*)buf,0x00,sl->sector[index].bl_size); //сформировать пакет для записи и сгенерить crc16 если требуется //копируем заголовок сегмента в массив для записи i=0; memcpy((void*)buf,(void*)&bl->body.pxNextFreeBlock,sl->sector[index].StartAddrLen); i=sl->sector[index].StartAddrLen; memcpy((void*)(buf+i),(void*)&bl->body.xBlockSize,sl->sector[index].SectorSizeLen); i+=sl->sector[index].SectorSizeLen; if((sl->sector[index].Type & SECTOR_CRC)>0) { Crc16_Clear(); crc=Crc16(buf,i); memcpy((void*)(buf+i),(void*)&crc,sizeof(UINT16_T)); } i=ERR_OK; i=sector_write(index, bl->pxCurrentAddr, (void*)buf, sl->sector[index].bl_size); local_free(buf); return i; }
static int mbox_write_verify(struct sbd_context *st, int mbox, struct sector_mbox_s *s_mbox) { void *data; int rc = 0; if (sector_write(st, MBOX_TO_SECTOR(mbox), s_mbox) < 0) return -1; data = sector_alloc(); if (sector_read(st, MBOX_TO_SECTOR(mbox), data) < 0) { rc = -1; goto out; } if (memcmp(s_mbox, data, sector_size) != 0) { cl_log(LOG_ERR, "Write verification failed!"); rc = -1; goto out; } rc = 0; out: free(data); return rc; }
static int header_write(struct sbd_context *st, struct sector_header_s *s_header) { s_header->sector_size = htonl(s_header->sector_size); s_header->timeout_watchdog = htonl(s_header->timeout_watchdog); s_header->timeout_allocate = htonl(s_header->timeout_allocate); s_header->timeout_loop = htonl(s_header->timeout_loop); s_header->timeout_msgwait = htonl(s_header->timeout_msgwait); return sector_write(st, 0, s_header); }
/*-----------------------------------------------------------*/ UINT8_T sector_MainSave() { UINT8_T i; UINT16_T size,size1; UINT8_T err; if(sl==NULL) return ERR_SL_NULL; //найти сектор main for(i=0;i<(sl->sector_counter);i++) { if((sl->sector[i].Type & SECTOR_MAIN)>0) { //сгенерировать CRC16 sl->crc=~sl->sector_counter; Crc16_Clear(); Crc16((UINT8_T*)&sl->sector_counter,sizeof(UINT8_T)); Crc16((UINT8_T*)&sl->crc,sizeof(UINT8_T)); sl->crc16=Crc16((UINT8_T*)sl->sector, (sizeof(SectorInfo)*sl->sector_counter) ); //размер структуры с учетом выравнивания size=( (sizeof(SectorList)-sizeof(SectorInfo*) + (sl->sector[i].ByteAligment-1)) & ~(sl->sector[i].ByteAligment-1) ); err=sector_write(i, sl->sector[i].StartAddr, (void*)sl, size); if(err!=ERR_OK) return err; size1=( (sizeof(SectorInfo)*sl->sector_counter + (sl->sector[i].ByteAligment-1)) & ~(sl->sector[i].ByteAligment-1) ); err=sector_write(i, sl->sector[i].StartAddr+size, (void*)sl->sector , size1); if(err!=ERR_OK) return err; return ERR_OK; } } return ERR_NO_MAIN; }
bool sector_write_n(void *self, uint8_t *buf, daddr_t sector, int count) { int i; for (i = 0; i < count; i++) { if (!sector_write(self, buf, sector)) return false; buf += DEV_BSIZE; sector++; } return true; }
/** * Write invalid logpack header. * This just fill zero. * * @fd file descriptor of data device (opened). * @super_sect super sector. * @lsid lsid to invalidate. * * RETURN: * true in success, or false. */ bool write_invalid_logpack_header( int fd, const struct sector_data *super_sect, u64 lsid) { struct sector_data *sect; bool ret; const struct walb_super_sector *super = get_super_sector_const(super_sect); u64 off = get_offset_of_lsid_2(super, lsid); sect = sector_alloc_zero(super->physical_bs); if (!sect) { LOGe("Allocate sector failed.\n"); return false; } ret = sector_write(fd, off, sect); if (!ret) { LOGe("Write sector %"PRIu64" for lsid %"PRIu64" failed.\n", off, lsid); } sector_free(sect); return ret; }
/** * Write multiple sectors data at an offset. * * RETURN: * true in success, or false. */ bool sector_array_pwrite( int fd, u64 offset, const struct sector_data_array *sect_ary, unsigned int start_idx, unsigned int n_sectors) { unsigned int i; ASSERT(fd > 0); ASSERT_SECTOR_DATA_ARRAY(sect_ary); ASSERT(start_idx + n_sectors <= sect_ary->size); for (i = 0; i < n_sectors; i++) { unsigned int idx = start_idx + i; u64 off = offset + i; bool ret = sector_write( fd, off, get_sector_data_in_array_const(sect_ary, idx)); if (!ret) { LOGe("write failed.\n"); return false; } } return true; }
static int mbox_write(struct sbd_context *st, int mbox, struct sector_mbox_s *s_mbox) { return sector_write(st, MBOX_TO_SECTOR(mbox), s_mbox); }
static int slot_write(struct sbd_context *st, int slot, struct sector_node_s *s_node) { return sector_write(st, SLOT_TO_SECTOR(slot), s_node); }