int TCRYPT_read_phdr(struct crypt_device *cd, struct tcrypt_phdr *hdr, struct crypt_params_tcrypt *params) { struct device *base_device, *device = crypt_metadata_device(cd); ssize_t hdr_size = sizeof(struct tcrypt_phdr); char *base_device_path; int devfd = 0, r, bs; assert(sizeof(struct tcrypt_phdr) == 512); log_dbg("Reading TCRYPT header of size %zu bytes from device %s.", hdr_size, device_path(device)); bs = device_block_size(device); if (bs < 0) return bs; if (params->flags & CRYPT_TCRYPT_SYSTEM_HEADER && crypt_dev_is_partition(device_path(device))) { base_device_path = crypt_get_base_device(device_path(device)); log_dbg("Reading TCRYPT system header from device %s.", base_device_path ?: "?"); if (!base_device_path) return -EINVAL; r = device_alloc(&base_device, base_device_path); if (r < 0) return r; devfd = device_open(base_device, O_RDONLY); free(base_device_path); device_free(base_device); } else
void add_devices(size_t num) { char name[8]; for (size_t i=0; i<num; i++) { re_snprintf(name, sizeof(name), "%c", init_char++); err = device_alloc(NULL, &devicel, name); ASSERT_EQ(0, err); } }
int LUKS2_hdr_version_unlocked(struct crypt_device *cd, const char *backup_file) { struct { char magic[LUKS2_MAGIC_L]; uint16_t version; } __attribute__ ((packed)) hdr; struct device *device = NULL; int r = 0, devfd = -1, flags; if (!backup_file) device = crypt_metadata_device(cd); else if (device_alloc(&device, backup_file) < 0) return 0; if (!device) return 0; flags = O_RDONLY; if (device_direct_io(device)) flags |= O_DIRECT; devfd = open(device_path(device), flags); if (devfd < 0) goto err; if ((read_lseek_blockwise(devfd, device_block_size(device), device_alignment(device), &hdr, sizeof(hdr), 0) == sizeof(hdr)) && !memcmp(hdr.magic, LUKS2_MAGIC_1ST, LUKS2_MAGIC_L)) r = (int)be16_to_cpu(hdr.version); err: if (devfd != -1) close(devfd); if (backup_file) device_free(device); return r; }
/* Make OS device a ZIP device. */ void zip_mount_os(void) { uint8 buffer[256]; uint8* szZipArchiveName; DEVICEPARAM p_readonly = { STRING_AND_LENGTH("ReadOnly"), ParamBoolean, NULL }; DEVICEPARAM p_crc32 = { STRING_AND_LENGTH("CheckCRC32"), ParamBoolean, NULL }; DEVICEPARAM p_filename = { STRING_AND_LENGTH("Filename"), ParamString, NULL }; /* Mount new swzipread device so we can read the zipped SW folder */ swzipreaddevice = device_alloc(STRING_AND_LENGTH(SWZIP_READ_DEV_NAME)) ; if ( !device_connect(swzipreaddevice, SWZIPREAD_DEVICE_TYPE, SWZIP_READ_DEV_NAME, DEVICEUNDISMOUNTABLE|DEVICEENABLED, TRUE)) { device_free(swzipreaddevice) ; swzipreaddevice = NULL ; } else { /* Add ZIP read device. */ device_add(swzipreaddevice); } /* If we have managed to mount an SW ZIP read device, assume that we MUST have a zipped SW folder to use. */ if (swzipreaddevice != NULL) { /* Rename %os% to something else */ theIDevName(osdevice) = (uint8*)SWZIP_WRITE_DEV_NAME; /* Mount new os device as ZIP device */ if ( ! device_connect(osdevice, ZIP_DEVICE_TYPE, "os", DEVICEUNDISMOUNTABLE|DEVICEENABLED, TRUE)) { device_free(swzipreaddevice) ; swzipreaddevice = NULL ; (void)dispatch_SwExit(swexit_error_zipsw_init, "Cannot initialise ZIP os device"); return; } /* Make ZIP OS device first device */ device_add_first(osdevice); /* NOTE: can't do this here since ZIP device calls SwOftenUnsafe which * depends on SystemParams, but that has not been set up yet! * Originally done in doBootup() which was last thing before interpreter * kicks off, so ideally need hook from there! */ /* ZIP device is already mounted enabled - need to turn on checksum checking * and modifiable */ theDevParamBoolean(p_crc32) = TRUE; if ( ((theISetParam(osdevice))(osdevice, &p_crc32) != ParamAccepted)) { (void)dispatch_SwExit(swexit_error_zipsw_config_01, "Failed to configure OS device(1)"); return; } theDevParamBoolean(p_readonly) = FALSE; if ( ((theISetParam(osdevice))(osdevice, &p_readonly) != ParamAccepted )) { (void)dispatch_SwExit(swexit_error_zipsw_config_02, "Failed to configure OS device(2)"); return; } szZipArchiveName = getZipReadArchiveName (buffer, sizeof (buffer)); theDevParamString(p_filename) = szZipArchiveName; theDevParamStringLen(p_filename) = strlen_int32 ((char*) szZipArchiveName); if ( ((theISetParam(osdevice))(osdevice, &p_filename) != ParamAccepted ) ) { (void)dispatch_SwExit(swexit_error_zipsw_config_03, "Failed to configure OS device(3)"); return; } } else { /* We did not manage to mount a SW ZIP read device, so just assume a normal SW folder. Do nothing. */ } } /* zip_mount_os */