/* re opens config file for readwrite with global name. The path is assumed * to be full or is relative to the startup drawer */ static Errcode open_config(XFILE **pxf, Boolean create) { Errcode err; FilePath *filepath; char odir[PATH_SIZE]; const char *config_name = vb.config_name; if (!pj_assert(vb.config_name != NULL)) return Err_abort; filepath = filepath_create_from_string(vb.init_drawer); if (filepath != NULL) { err = filepath_append(filepath, vb.config_name); if (err == Success) err = filepath_to_cstr(filepath, DIR_DELIM, odir, sizeof(odir)); if (err == Success) config_name = odir; filepath_destroy(filepath); } return xffopen(config_name, pxf, create ? XREADWRITE_CLOBBER : XREADWRITE_OPEN); }
void firm_save(firm_context* ctx, u32 index, u32 flags) { firm_sectionheader* section = (firm_sectionheader*)(ctx->header.section + index); u32 offset; u32 size; u32 address; FILE* fout; filepath outpath; u8 buffer[16 * 1024]; offset = getle32(section->offset); size = getle32(section->size); address = getle32(section->address); filepath_copy(&outpath, settings_get_firm_dir_path(ctx->usersettings)); filepath_append(&outpath, "firm_%d_%08X.bin", index, address); if (size == 0 || outpath.valid == 0) return; if (size >= ctx->size) { fprintf(stderr, "Error, firm section %d size invalid\n", index); return; } fout = fopen(outpath.pathname, "wb"); if (fout == 0) { fprintf(stderr, "Error, failed to create file %s\n", outpath.pathname); goto clean; } fseek(ctx->file, ctx->offset + offset, SEEK_SET); fprintf(stdout, "Saving section %d to %s...\n", index, outpath.pathname); while(size) { u32 max = sizeof(buffer); if (max > size) max = size; if (max != fread(buffer, 1, max, ctx->file)) { fprintf(stdout, "Error reading input file\n"); goto clean; } if (max != fwrite(buffer, 1, max, fout)) { fprintf(stdout, "Error writing output file\n"); goto clean; } size -= max; } clean: return; }