u_int32 write_file (u_int32 handle,u_int32 buf_len,void*buf,u_int32 send_pid ){ /*patch:判断权限*/ int i; /*patch:判断handle的有效性*/ int8*buffer=(int8*)transTolinerAddr((u_int32)buf,send_pid,1); u_int32 write_sects=buf_len/512; /*patch:增加判断扇区超出文件拥有扇区*/ u_int32 start=fat.i_node_arr[handle].startSector; u_int32 pos=0; for(i=start;i<start+write_sects;i++,pos+=512) while(write_sector(i,buffer+pos)==FALSE); /*文件若变得更长,则需要更新信息*/ u_int32 size=fat.i_node_arr[handle].byte_size; if(buf_len>size) fat.i_node_arr[handle].byte_size=buf_len; if((buf_len%512)==0) { return TRUE; } else {/*否则对最后一个扇区特殊处理*/ int8 tempBuf[512]; while(read_sector(start+write_sects,tempBuf)==FALSE); memcpy8(buffer+write_sects*512,tempBuf,buf_len%512); while(write_sector(start+write_sects,tempBuf)==FALSE); return TRUE; } }
void create_fs ( ){ /*进入此函数证明当前硬盘无任何操作系统,需要初始化superBlock,扇区位图,文件分配表*/ /*superBlock*/ memcpy8("TINY",superBlock.sign,5);/*文件系统标识*/ superBlock.n_sects=hd_info.sectorNum; superBlock.n_inode=MAX_FILE; /*扇区位图,注:前32个扇区存储文件系统的信息,一定处于使用状态*/ int i; for(i=0;i<DATA_START;i++) mask1(i); for(i=DATA_START;i<superBlock.n_sects;i++) mask0(i); /*文件分配表,初始时全部无效化*/ fat.size=0; for(i=0;i<MAX_FILE;i++) fat.i_node_arr[i].status=I_NODE_INVALID; /*将信息全部写入硬盘*/ u_int8 buf[512]; /*写superBlock*/ memcpy8((u_int8*)&superBlock,buf,SUPERBLOCK_SIZE); while(write_sector(SUPERBLOCK_START,buf)==FALSE); /*写位图*/ u_int32 bmp_sect=((superBlock.n_sects%4096)==0)?(superBlock.n_sects/4096):(superBlock.n_sects/4096+1); u_int32 pos=0; for(i=HD_BMP_START;i<HD_BMP_START+bmp_sect;i++,pos+=512) while(write_sector(i,hd_bmp+pos)==FALSE); /*写文件分配表*/ pos=0; for(i=FAT_START;i<FAT_START+MAX_FAT;i++,pos+=512) while(write_sector(i,((u_int8*)&fat+pos))==FALSE); }
int allocate_cluster(unsigned int cluster) { int retVal=0; do { int fat_sector_num=cluster/(512/4); int fat_sector_offset=(cluster*4)&0x1FF; if (fat_sector_num>=sectors_per_fat) { printf("ERROR: cluster number too large.\n"); retVal=-1; break; } // Read in the sector of FAT1 unsigned char fat_sector[512]; if (read_sector(partition_start+fat1_sector+fat_sector_num,fat_sector,0)) { printf("ERROR: Failed to read sector $%x of first FAT\n",fat_sector_num); retVal=-1; break; } dump_bytes(0,"FAT sector",fat_sector,512); printf("Marking cluster $%x in use by writing to offset $%x of FAT sector $%x\n", cluster,fat_sector_offset,fat_sector_num); // Set the bytes for this cluster to $0FFFFF8 to mark end of chain and in use fat_sector[fat_sector_offset+0]=0xf8; fat_sector[fat_sector_offset+1]=0xff; fat_sector[fat_sector_offset+2]=0xff; fat_sector[fat_sector_offset+3]=0x0f; printf("Marking cluster in use in FAT1\n"); // Write sector back to FAT1 if (write_sector(partition_start+fat1_sector+fat_sector_num,fat_sector)) { printf("ERROR: Failed to write updated FAT sector $%x to FAT1\n",fat_sector_num); retVal=-1; break; } printf("Marking cluster in use in FAT2\n"); // Write sector back to FAT2 if (write_sector(partition_start+fat2_sector+fat_sector_num,fat_sector)) { printf("ERROR: Failed to write updated FAT sector $%x to FAT1\n",fat_sector_num); retVal=-1; break; } printf("Done allocating cluster\n"); } while(0); return retVal; }
/*函数*/ void write_back (/*关机之前写回表格信息*/ ){ /*写superBlock*/ while(write_sector(SUPERBLOCK_START,(u_int8*)&superBlock)==FALSE); /*写位图*/ u_int32 bmp_sect=((superBlock.n_sects%4096)==0)?(superBlock.n_sects/4096):(superBlock.n_sects/4096+1); u_int32 pos=0,i; for(i=HD_BMP_START;i<HD_BMP_START+bmp_sect;i++,pos+=512) while(write_sector(i,hd_bmp+pos)==FALSE); /*写文件分配表*/ pos=0; for(i=FAT_START;i<FAT_START+MAX_FAT;i++,pos+=512) while(write_sector(i,((u_int8*)&fat+pos))==FALSE); }
void format() { // first we clear all the space in the drive int cyl, sect; char blank[BYTES_PER_SECTOR] = ""; for(cyl = 0; cyl < CYLINDERS; ++cyl) { for(sect = 0; sect < SECTORS_PER_CYLINDER; ++sect) { int err = write_sector(cyl, sect, blank); if(err == BAD_CYLINDER) { printf("Bad cylinder: %d\n", cyl); } else if(err == BAD_SECTOR) { printf("Bad sector: %d\n", sect); } } } // now we set up the initial fat //struct fat *initial_fat = malloc(FAT_SIZE); //struct dir *root_dir = malloc(sizeof(struct dir)); struct fs *init_fs = malloc(sizeof(struct fs)); for(int i = 0; i < TOTAL_SECTORS; ++i) { init_fs->the_fat.table[i] = EMPTY; } memset(&init_fs->root_dir, 0, sizeof(struct dir)); store_fs(init_fs); free(init_fs); }
static int decode_C1(void) { int secoff,tmpoff,c; tmpoff = fgetc(fin); if( tmpoff == EOF) { show_file_error( fin ); return 0; } c=tmpoff; for (secoff=0; secoff<=tmpoff; secoff++) { buf[c]=fgetc(fin); c--; if( feof(fin) ) { show_file_error( fin ); return 0; } } if( !write_sector(fout) ) return 0; return 1; }
// FSM void disk_sched_irq_handler(){ irq_disable(); disk_job_str* current = disk_queue; assert(current); ctx_s* owner = current->ctx; _dprint("DISK_IRQ\n"); switch(disk_state){ case DS_IDLE: fprintf(stderr, "WARNING - %s:%d - UNEXPECTED INTERRUPT IN IDLE STATE.\n", __FILE__, __LINE__); return; case DS_SEEK:{ _dprint("SEEK TERMINE\n"); // on vient de finir le seek. // Que faire maintenant ? > Dépend du type de tâche switch(current->type){ case DJT_READ: // on lance le read disk_state = DS_READ; read_sector(current->cyl, current->sect, current->buffer); irq_enable(); return; case DJT_WRITE: // on lance le write disk_state = DS_WRITE; write_sector(current->cyl, current->sect, current->buffer); irq_enable(); return; case DJT_FORMAT: // on lance le format disk_state = DS_FORMAT; mFormat(current->cyl, current->sect, 1, 0x0); irq_enable(); return; default: // ??? return; } } case DS_READ: // les données sont présentes dans le MASTERBUFFER. On les copie dans l buffer du job _dprint("READ TERMINE\n"); assert(current->buffer); memcpy(current->buffer, MASTERBUFFER, secSize); case DS_WRITE: case DS_FORMAT: //disk_delete_job(current); disk_queue = current->next; // retour à idle disk_state = DS_IDLE; disk_ctx->status = CTXS_ACTIVABLE; // réactiver contexte propriétaire. owner->status = CTXS_ACTIVABLE; _dprint("JOB DONE\n"); irq_enable(); return; default: return; } }
static int write_inode(struct file_buffer *fb) { struct lmfs_inode_entry *lp = (struct lmfs_inode_entry *)fb->inode_entry; char *data_buffer = fb->data_buffer; u32 *block_buffer = (u32 *)fb->block_buffer; int i; debug("write inode %d\n", fb->inode); lp->pos_in_lvl1 = fb->lvl1_pos; lp->pos_in_lvl0 = fb->lvl0_pos; /* fix me some conner case will block * data write to the file * should consider the fb->data_pos and the * fb->lvl1_pos. TBD later */ if (fb->data) flush_data(data_buffer, fb->data); debug("lvl1 %d buf[0] %d\n", fb->lvl1, block_buffer[0]); if (fb->lvl1) flush_data((char *)block_buffer, fb->lvl1); i = inode_to_sector(fb->inode); /* first copy the inode meta data, then copy the block info */ //memcpy(data_buffer, lp, sizeof(struct lmfs_inode_entry) - 64 * sizeof(u32)); //memcpy(data_buffer + 256, lp->lvl1_block, 256); write_sector((char *)lp, i); return 0; }
void programmeTest(){ int i,j; unsigned char buffer[HDA_SECTORSIZE]; initialisation(); printf("Test de dmps() : \n"); for(i=0;i<HDA_MAXCYLINDER;i++) for(j=0;j<HDA_MAXSECTOR;j++) dmps(i,j); printf("\nTest de frmt() : \n"); for(i=0;i<HDA_MAXCYLINDER;i++) for(j=0;j<HDA_MAXSECTOR;j++) dmps(i,j); init_hardware("hardware.ini"); printf("\n\n Réinitialisation du disque... \n"); printf("\n\nTest de read_sector() : \n"); for(i=0;i<HDA_MAXCYLINDER;i++){ for(j=0;j<HDA_MAXSECTOR;j++){ read_sector(i,j,buffer); } for(j=0;j<HDA_SECTORSIZE;j++) printf("%x ",buffer[i]); printf("\n"); } printf("\n\nTest de write_sector() : \n"); for(i=0;i<HDA_MAXCYLINDER;i++){ for(j=0;j<HDA_MAXSECTOR;j++){ write_sector(i,j,buffer); } for(j=0;j<HDA_SECTORSIZE;j++) printf("%x ",buffer[i]); printf("\n"); } printf("\n\nTest de format_sector() : \n"); for(i=0;i<HDA_MAXCYLINDER;i++){ for(j=0;j<HDA_SECTORSIZE;j++) buffer[j]=i; for(j=0;j<HDA_MAXSECTOR;j++){ write_sector(i,j,buffer); } for(j=0;j<HDA_SECTORSIZE;j++) printf("%x ",buffer[i]); printf("\n"); } }
static int decode_C7(void) { if( !read_sector(fin) ) return 0; if( !write_sector(fout) ) return 0; return 1; }
void UPD765A::write_data(bool deleted) { if((result = check_cond(true)) != 0) { shift_to_result7(); return; } result = write_sector(deleted); return; }
int store_fs(struct fs *the_fs) { size_t size = sizeof(struct fs); char *data = malloc(sizeof(char) * size); memcpy(data, the_fs, size); size_t offset = 0; int cur_sect = 0; for(int i = 0; i < size / BYTES_PER_SECTOR; ++i) { write_sector(0, cur_sect, data + offset); cur_sect++; offset += BYTES_PER_SECTOR; } char leftover[BYTES_PER_SECTOR]; memcpy(leftover, data + offset, size - offset); write_sector(1, cur_sect, leftover); free(data); return 0; }
void write_bloc(const unsigned int vol, const unsigned int nbloc, const unsigned char* buffer) { unsigned int secteur, cylindre; calc_secteur_cylindre(vol, nbloc, §eur, &cylindre); write_sector(cylindre, secteur, buffer); }
// this func writes a cluster of 4096 bytes 8 sector of 512 bytes u_int8_t write_cluster(u_int32_t lcn, cluster_t buff) { u_int32_t sector_num = lcn*CLUSTER_DIM; u_int8_t i; for(i=0; i<CLUSTER_DIM; i++) { //printf("writing sector %d\n", i); write_sector(buff+(i*512), DRIVE, sector_num+i, N_SECTOR);// read cluster of 4096 bytes delay(10); } return 0; }
static void* writeThreadRun(void* args) { while(1) { struct voucher* v = (struct voucher*) blockingReadBB(readVoucherBuffer); int status = write_sector(diskDevice, &(v->sector)); pthread_mutex_lock(&(v->voucherMutex)); v->status = status; pthread_cond_signal(&(v->voucherCond)); pthread_mutex_unlock(&(v->voucherMutex)); } pthread_exit(NULL); }
static void sgi_write_table(void) { sgilabel->csum = 0; sgilabel->csum = SGI_SSWAP32(two_s_complement_32bit_sum( (unsigned int*)sgilabel, sizeof(*sgilabel))); assert(two_s_complement_32bit_sum( (unsigned int*)sgilabel, sizeof(*sgilabel)) == 0); write_sector(0, sgilabel); if (is_prefixed_with((char*)sgilabel->directory[0].vol_file_name, "sgilabel")) { /* * keep this habit of first writing the "sgilabel". * I never tested whether it works without (AN 981002). */ sgiinfo *info = fill_sgiinfo(); int infostartblock = SGI_SSWAP32(sgilabel->directory[0].vol_file_start); write_sector(infostartblock, info); free(info); } }
void write_mbr() { unsigned char buffer[HDA_SECTORSIZE]; assert(mbr.magic == MAGIC); memset(buffer, HDA_SECTORSIZE, 0); memcpy(buffer, &mbr, sizeof(struct mbr_s)); write_sector(0, 0, buffer); }
u8 go_umount(u8 dev){ u8 i,bufr; struct super_sect sect8; knock_out(dev); all_out(dev); sect8.block=super[get_super(dev)] ; buf[bufr].data.ini.sb = super[get_super(dev)] ; for (i=0;i < 45 ;i++ ){ sect8.bit_mapa[i] = bit_map[i]; } write_sector( 0,0,8,(u8 *)§8); super[get_super(dev)].dev=NO_DEV; return 1; }
void format_sector(unsigned int piste,unsigned int secteur,unsigned int nsecteur,unsigned int value){ int i; unsigned char buffer[HDA_SECTORSIZE]; for(i=0;i<HDA_SECTORSIZE;i++) buffer[i] = value; for(i=secteur;i< secteur+ nsecteur;i++) write_sector(piste,i,buffer); if(verbose) printf("\nLe formatage du secteur s'est déroulé avec succés\n"); }
static int decode_C3(void) { int secoff,tmpoff,c; secoff=0; do { if (secoff) tmpoff = read_offset(fin); else tmpoff = fgetc(fin); if( tmpoff == EOF ) { show_file_error( fin ); return 0; } for (; secoff<tmpoff; secoff++) { buf[secoff] = fgetc(fin); if( feof(fin) ) { show_file_error( fin ); return 0; } } if (secoff == (int)secsize) break; tmpoff = read_offset(fin); c = fgetc(fin); if( tmpoff == EOF || c == EOF ) { show_file_error( fin ); return 0; } for (; secoff<tmpoff; secoff++) { buf[secoff] = c; } } while(secoff < (int)secsize); if( !write_sector(fout) ) return 0; return 1; }
void frmt(){ int i,j; unsigned char buffer[HDA_SECTORSIZE]; for(i=0;i<HDA_SECTORSIZE;i++){ buffer[i]= 0 ; } for(i=0;i<HDA_MAXCYLINDER;i++){ for(j=0;j<HDA_MAXSECTOR;j++){ write_sector(i,j,buffer); } } if(verbose) printf("\nLe formatage s'est déroulé avec succés\n"); }
void dos_write_table(struct fdisk_context *cxt) { int i; /* MBR (primary partitions) */ if (!MBRbuffer_changed) { for (i = 0; i < 4; i++) if (ptes[i].changed) MBRbuffer_changed = 1; } if (MBRbuffer_changed) { write_part_table_flag(cxt->mbr); write_sector(cxt, 0, cxt->mbr); } /* EBR (logical partitions) */ for (i = 4; i < partitions; i++) { struct pte *pe = &ptes[i]; if (pe->changed) { write_part_table_flag(pe->sectorbuffer); write_sector(cxt, pe->offset, pe->sectorbuffer); } } }
void really_write_sector (int fd, unsigned int sector){ int i; if (sector >= 7) { fprintf(stderr,"ERROR: Write sector %d is forbidden\n", sector); return; }; for (i=0; i<10; i++){ if (0 == write_sector(fd, sector)) return; }; fprintf(stderr,"ERROR: Could not write sector %d\n", sector); exit(20); };
static void* writeThreadRun(void* args) { while(1) { struct Voucher* v; int readFromBufferStatus = nonblockingReadBB(writeBuffer, (void*)&v); if (readFromBufferStatus == 1){ //if readFromBufferStatus is 1, it meant the buffer wasn't empty and it had a voucher w/ sd int status = write_sector(diskDev, &(v->sector)); pthread_mutex_lock(&(v->vouchMut)); v->status = status; pthread_mutex_unlock(&(v->vouchMut));//lock so it cant be changed pthread_cond_signal(&(v->vouchCond));//unlock } //else if the buffer was empty just do nothing and loop back round } return 0; }
/* * Build a minimal VDM label and boot area. * Allows you to shoot yourself in the foot, badly. */ void initialize(int fd) { uint8_t sector[VDM_BLOCK_SIZE]; struct vdm_label dl; struct vdm_boot_info bi; memset(sector, 0, sizeof sector); memset(&dl, 0, sizeof dl); memset(&bi, 0, sizeof bi); dl.signature = htobe32(VDM_LABEL_SIGNATURE); bi.signature = htobe32(VDM_LABEL_SIGNATURE); bi.boot_start = htobe32(8); bi.boot_size = htobe32(VDM_BOOT_DEFAULT_SIZE); bi.version = htobe32(VDM_BOOT_INFO_VERSION); memcpy(sector + VDM_LABEL_OFFSET_ALT, &dl, sizeof dl); memcpy(sector + VDM_BLOCK_SIZE - sizeof bi, &bi, sizeof bi); write_sector(fd, VDM_LABEL_SECTOR, sector); }
static int decode_C4(void) { int secoff,tmpoff; tmpoff = read_offset(fin); if( tmpoff == EOF ) { show_file_error( fin ); return 0; } for (secoff=tmpoff; secoff<(int)secsize; secoff++) { buf[secoff]=fgetc(fin); if( feof(fin) ) { show_file_error( fin ); return 0; } } if( !write_sector(fout) ) return 0; return 1; }
int main(int argc,char *argv[]) { FILE *fd; char sectbuf[256]; int sector,read,err; if (argc!=4) { printf("Usage: writeblock disk_image blockfile sector_no\n"); printf("\nThis tool will write <blockfile> onto side 2 of a double-sided disk image,\n"); printf("starting at sector sector_no (zero-based).\n\n"); printf("Caution:\n"); printf("\t-the disk should have been previously formatted (17 sectors per track)\n"); printf("\t-the logical sector number should not exceed the capacity of one side\n"); exit(1); } if (open_disk(argv[1])) { printf("Cannot open disk image %s, or invalid disk image.\n",argv[1]); exit(2); } fd=fopen(argv[2],"rb"); if (fd==NULL) { printf("Cannot open blockfile %s\n",argv[2]); exit(2); } sector=atoi(argv[3]); read=fread(sectbuf,1,256,fd); while (read>0) { err=write_sector(sectbuf,sector/17,1,sector%17+1); if (err) { printf("Cannot write sector, error %d\n",err); exit(4); } sector++; read=fread(sectbuf,1,256,fd); } close_disk(); }
int main( int argc, char *argv[] ) { int i; diag_printf("FLASH here!\n"); while (identify_FLASH() == 0) { diag_printf("... Please change FLASH jumper\n"); cyg_thread_delay(5*100); } diag_printf("About to program FLASH using data at %x..%x\n", flash_buffer, flash_buffer_end); diag_printf("*** Press RESET now to abort!\n"); cyg_thread_delay(5*100); diag_printf("\n"); diag_printf("...Programming FLASH\n"); i = 0; while (flash_buffer < flash_buffer_end) { write_sector(i++, flash_buffer); flash_buffer += 256; } diag_printf("All done!\n"); while (1) ; }
/*--------------------------------------------------------*/ int main(int argc, char *argv[]) { char *parfname, *instrname, *maskname, *reffname, *imgfname, **inpfname, **outfname, **kerfname; unsigned short *mask0, *mask1, *flag0, *flag1; int *indx, nsec_x, nsec_y, isec_x, isec_y, x_off, y_off, mode, deg[MAX_NCOMP], i, k, nclip, npix, nim, ntab, nkeep; long int imglen, buflen, veclen, intlen, matlen, usblen, lomlen, lovlen, tablen, domlen, srtlen, *headlen, mheadlen, rheadlen; float *im, *imref, *imdif, sig[MAX_NCOMP], *sort_buf; double **vecs, **mat, *vec, **tab00, **wxy, *ker_norm, chi2_n, good_area, total_area, parity; DOM_TABS *domp; PARAMS par; /**************************************************************************/ imref = imdif = NULL; mask0 = mask1 = NULL; flag0 = flag1 = NULL; indx = NULL; vec = NULL; vecs = mat = NULL; tab00 = wxy = NULL; sort_buf = NULL; good_area = 0.0; domp = 0; /*** argument handling ***/ if (argc != 6) usage(); parfname = argv[1]; instrname= argv[2]; maskname = argv[3]; reffname = argv[4]; imgfname = argv[5]; /**************************************************************************/ /*** get parameters ***/ /**************************************************************************/ par.deg = deg; par.sig = sig; get_params(parfname, instrname, &par); if (par.verbose > 3) { printf("parfname: %s\n", parfname); printf("instrname: %s\n", instrname); printf("maskname: %s\n", maskname); printf("reffname: %s\n", reffname); printf("imgfname: %s\n", imgfname); printf("--------------\n"); } /*** get the list of input, output and kernel files ***/ if (par.verbose > 2) printf("Reading '%s'\n", imgfname); nim=read_list(par, imgfname, &inpfname, &outfname, &kerfname); if (par.verbose > 2) printf("%d file-names read read\n", nim); for (i=1; i<par.ncomp; i++) { par.deg[i] = par.deg[i-1] + par.deg_inc; par.sig[i] = par.sig[i-1] * par.sig_inc; } par.nx += 2*par.kerhw; par.ny += 2*par.kerhw; par.sdeg = par.wdeg; if (par.bdeg > par.wdeg) par.sdeg = par.bdeg; par.nwxy = (par.wdeg+1)*(par.wdeg+2)/2; par.nspat = (par.sdeg+1)*(par.sdeg+2)/2; par.nbkg = (par.bdeg+1)*(par.bdeg+2)/2; par.nvecs = par.nbkg; for (i=0; i<par.ncomp; i++) par.nvecs += (par.deg[i]+1)*(par.deg[i]+2)/2; par.ntot = par.nvecs + (par.nvecs - par.nbkg - 1)*(par.nwxy - 1); par.ndom = par.ndom_x*par.ndom_y; ntab = par.nvecs-par.nbkg+par.nspat; if (par.verbose) printf("\t %d image(s) to process\n\n", nim); /**************************************************************************/ /*** get memory ***/ /**************************************************************************/ imglen = par.nx*par.ny*sizeof(float); buflen = par.nx*par.ny*sizeof(double); matlen = par.ntot*sizeof(double *); veclen = par.ntot*sizeof(double); usblen = par.nx*par.ny*sizeof(unsigned short); tablen = ntab*sizeof(double *); lomlen = par.nvecs*sizeof(double *); lovlen = par.nvecs*sizeof(double); domlen = par.ndom*sizeof(DOM_TABS); srtlen = par.ndom*sizeof(float); intlen = par.ndom*sizeof(int); if (par.ntot > par.ndom) intlen = par.ntot*sizeof(int); if (!(im = (float *)malloc(imglen))) errmess("malloc(im)"); if (!(imref = (float *)malloc(imglen))) errmess("malloc(imref)"); if (!(imdif = (float *)malloc(imglen))) errmess("malloc(imdif)"); if (!(mat = (double **)malloc(matlen))) errmess("malloc(mat)"); if (!(tab00 = (double **)malloc(tablen))) errmess("malloc(tab00)"); if (!(vecs = (double **)malloc(tablen))) errmess("malloc(vecs)"); if (!(wxy = (double **)malloc(tablen))) errmess("malloc(wxy)"); if (!(domp = (DOM_TABS *)malloc(domlen))) errmess("malloc(DOM_TABS)"); if (!(sort_buf = (float *)malloc(srtlen))) errmess("malloc(sort_buf)"); for (i=0; i<ntab; i++) if (!(tab00[i] = (double *)malloc(buflen))) errmess("malloc(tab00[i])"); for (k=0; k<par.ndom; k++) { if (!(domp[k].mat0 = (double **)malloc(lomlen))) errmess("malloc(domp[k].mat0)"); if (!(domp[k].mat1 = (double **)malloc(lomlen))) errmess("malloc(domp[k].mat1)"); if (!(domp[k].mat = (double **)malloc(lomlen))) errmess("malloc(domp[k].mat)"); if (!(domp[k].vec0 = (double *)malloc(lovlen))) errmess("malloc(domp[k].vec0)"); if (!(domp[k].vec = (double *)malloc(lovlen))) errmess("malloc(domp[k].vec)"); for (i=0; i<par.nvecs; i++) { if (!(domp[k].mat0[i] = (double *)malloc(lovlen))) errmess("malloc(domp[k].mat0[i])"); if (!(domp[k].mat1[i] = (double *)malloc(lovlen))) errmess("malloc(domp[k].mat1[i])"); if (!(domp[k].mat[i] = (double *)malloc(lovlen))) errmess("malloc(domp[k].mat[i])"); } } /* mat is indexed from 1 to n for use with numerical recipes ! */ for (i=0; i<par.ntot; i++) { if (!(mat[i] = (double *)malloc(veclen))) errmess("malloc(mat[i])"); mat[i]--; } mat--; if (!(vec = (double *)malloc(veclen))) errmess("malloc(vec)"); if (!(indx = (int *)malloc(intlen))) errmess("malloc(indx)"); if (!(mask0 = (unsigned short *)malloc(usblen))) errmess("malloc(mask0)"); if (!(mask1 = (unsigned short *)malloc(usblen))) errmess("malloc(mask1)"); if (!(flag0 = (unsigned short *)malloc(usblen))) errmess("malloc(flag0)"); if (!(flag1 = (unsigned short *)malloc(usblen))) errmess("malloc(flag1)"); if (!(headlen=(long *)calloc(nim, sizeof(long)))) errmess("calloc(headlen)"); if (!(ker_norm=(double *)calloc(nim, sizeof(double)))) errmess("calloc(ker_norm)"); /**************************************************************************/ /**************************************************************************/ /* get information about header sizes */ mheadlen=get_headlen(maskname); rheadlen=get_headlen(reffname); init_difimages(inpfname, outfname, headlen, nim, par); /**************************************************************************/ if (par.verbose > 4) { printf("par.nx0= %d par.ny0 = %d\n", par.nx0, par.ny0); printf("par.kerhw= %d\n", par.kerhw); printf("par.nx= %d par.ny = %d\n", par.nx, par.ny); } nsec_x = (par.nx0 - 2*par.kerhw)/(par.nx - 2*par.kerhw); nsec_y = (par.ny0 - 2*par.kerhw)/(par.ny - 2*par.kerhw); /**************************************************************************/ /*** main loop over sections of each image ***/ /**************************************************************************/ if (par.verbose > 4) printf("main loop over sections: nsec_x= %d nsec_y= %d\n", nsec_x, nsec_y); for (isec_x=0; isec_x<nsec_x; isec_x++) { for (isec_y=0; isec_y<nsec_y; isec_y++) { y_off = isec_y*(par.ny - 2*par.kerhw); x_off = isec_x*(par.nx - 2*par.kerhw); mode = (int)( isec_x || isec_y ); if (par.verbose > 4) printf("isec_x= %d isec_y= %d mode= %d\n", isec_x, isec_y, mode); read_sector(maskname, mheadlen, par.nx0, par.ny0, x_off, y_off, (char *)mask0, sizeof(unsigned short), par.nx, par.ny); read_sector(reffname, rheadlen, par.nx0, par.ny0, x_off, y_off, (char *)imref, sizeof(float), par.nx, par.ny); make_vectors(imref, tab00, vecs, wxy, par); mask_badpix(imref, mask0, flag0, 0, par); get_domains(imref, mask0, domp, par, &par.ndom); make_domains(imref, mask0, vecs, domp, par); total_area = (2*par.domhw + 1); total_area *= total_area*par.ndom; /**********************************************/ /*** loop over images for a given section ***/ /**********************************************/ for (i=0; i<nim; i++) { if (par.verbose >= VERB_MED) printf("\nSection [%d,%d] of image %d : %s\n", isec_x+1, isec_y+1, i+1, inpfname[i]); read_sector(inpfname[i], headlen[i], par.nx0, par.ny0, x_off, y_off, (char *)im, sizeof(float), par.nx, par.ny); mask_badpix(im, mask1, flag1, 1, par); npix = clean_domains(im, imref, mask0, mask1, vecs, domp, par); /*** sigma clipping ***/ chi2_n = BIG_FLOAT; if (par.verbose >= VERB_MED) { printf("\nLocal sigma clipping of domains:\n"); printf(" iteration chi2/dof N(clip) N(fit)\n"); } for (k=0; k<=par.n_iter+1; k++) { good_area = npix/total_area; if (good_area < par.min_area) break; clone_domains(domp, par.ndom, par.nvecs); /**************************/ if (k > 0) { nclip = local_clip(im, imref, mask1, vecs, wxy, vec, domp, par, &chi2_n, &npix); if (par.verbose >= VERB_MED) printf("%6d\t%14.4f\t%9d\t%7d\n", k-1, par.gain*chi2_n, nclip, npix); } /**************************/ expand_matrix(mat, vec, wxy, domp, par); ludcmp(mat, par.ntot, indx-1, &parity); lubksb(mat, par.ntot, indx-1, vec-1); } if (par.verbose >= VERB_MED) { printf("\nSigma clipping of domain distribution:\n"); printf("iteration median chi2/dof sigma N(good)\n"); } nkeep = par.ndom; for (k=1; k<=par.n_iter_dom && nkeep>=par.min_nkeep; k++) nkeep = clip_domains(domp, sort_buf, indx, k, &chi2_n, par); /*** sigma clipping ends ***/ chi2_n *= par.gain; /*** final solution ***/ if (good_area < par.min_area) { printf("\nFailed for section [%d,%d] of image %d : %s\n", isec_x+1, isec_y+1, i+1, inpfname[i]); printf("*** [ good_area= %f < %f ] ***\n", good_area, par.min_area); } else if (chi2_n > par.max_chi2) { printf("\nFailed for section [%d,%d] of image %d : %s\n", isec_x+1, isec_y+1, i+1, inpfname[i]); printf("*** [ chi2_n= %f > %f ] ***\n", chi2_n, par.max_chi2); } else if (nkeep < par.min_nkeep) { printf("\nFailed for section [%d,%d] of image %d : %s\n", isec_x+1, isec_y+1, i+1, inpfname[i]); printf("*** [ nkeep= %d < %d ] ***\n", nkeep, par.min_nkeep); } else { expand_matrix(mat, vec, wxy, domp, par); ludcmp(mat, par.ntot, indx-1, &parity); lubksb(mat, par.ntot, indx-1, vec-1); spatial_convolve(im, imdif, vecs, wxy, vec, par); /*** output ***/ ker_norm[i] = vec[par.nbkg]; apply_norm(imdif, flag0, flag1, ker_norm[i], par.nx, par.ny, par.bad_value); write_sector(outfname[i], headlen[i], par.nx0, par.ny0, x_off, y_off, (char *)imdif, sizeof(float), par.nx, par.ny, par.kerhw); } if (par.verbose) write_kernel(kerfname[i], vec, nsec_x, nsec_y, mode, par, &chi2_n); } /**********************************************/ /*** loop over images ends ***/ /**********************************************/ } } /**********************************************/ /*** loop over sections ends ***/ /**********************************************/ if (par.verbose >= VERB_HIGH) { printf("\nKernel norms:\n\n"); for (i=0; i<nim; i++) printf("%s \t %8.5f\n", kerfname[i], ker_norm[i]); printf("\n"); } for (i=0; i<nim; i++) { free(inpfname[i]); free(outfname[i]); free(kerfname[i]); } free(inpfname); free(outfname); free(kerfname); free(headlen); free(ker_norm); return(0); }
bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename) { u32 sectors_per_fat; u32 sectors_per_disk; // Convert MB to bytes disk_size *= 1024 * 1024; if (disk_size < 0x800000 || disk_size > 0x800000000ULL) { ERROR_LOG(COMMON, "Trying to create SD Card image of size %" PRIu64 "MB is out of range (8MB-32GB)", disk_size / (1024 * 1024)); return false; } // Pretty unlikely to overflow. sectors_per_disk = (u32)(disk_size / 512); sectors_per_fat = get_sectors_per_fat(disk_size, get_sectors_per_cluster(disk_size)); boot_sector_init(s_boot_sector, s_fsinfo_sector, disk_size, nullptr); fat_init(s_fat_head); File::IOFile file(filename, "wb"); FILE* const f = file.GetHandle(); if (!f) { ERROR_LOG(COMMON, "Could not create file '%s', aborting...", filename.c_str()); return false; } /* Here's the layout: * * boot_sector * fsinfo_sector * empty * backup boot sector * backup fsinfo sector * RESERVED_SECTORS - 4 empty sectors (if backup sectors), or RESERVED_SECTORS - 2 (if no backup) * first fat * second fat * zero sectors */ if (write_sector(f, s_boot_sector)) goto FailWrite; if (write_sector(f, s_fsinfo_sector)) goto FailWrite; if (BACKUP_BOOT_SECTOR > 0) { if (write_empty(f, BACKUP_BOOT_SECTOR - 2)) goto FailWrite; if (write_sector(f, s_boot_sector)) goto FailWrite; if (write_sector(f, s_fsinfo_sector)) goto FailWrite; if (write_empty(f, RESERVED_SECTORS - 2 - BACKUP_BOOT_SECTOR)) goto FailWrite; } else { if (write_empty(f, RESERVED_SECTORS - 2)) goto FailWrite; } if (write_sector(f, s_fat_head)) goto FailWrite; if (write_empty(f, sectors_per_fat - 1)) goto FailWrite; if (write_sector(f, s_fat_head)) goto FailWrite; if (write_empty(f, sectors_per_fat - 1)) goto FailWrite; if (write_empty(f, sectors_per_disk - RESERVED_SECTORS - 2 * sectors_per_fat)) goto FailWrite; return true; FailWrite: ERROR_LOG(COMMON, "Could not write to '%s', aborting...", filename.c_str()); if (unlink(filename.c_str()) < 0) ERROR_LOG(COMMON, "unlink(%s) failed: %s", filename.c_str(), GetLastErrorMsg().c_str()); return false; }