/*
Copyright 2022, Exotic Silicon, all rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. This software is licensed exclusively under this specific license text. The
license text may not be changed, and the software including modified versions
may not be re-licensed under any other license text.
2. Redistributions of source code must retain the above copyright notice, this
list of conditions, and the following disclaimer.
3. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions, and the following disclaimer in the documentation
and/or other materials provided with the distribution.
4. All advertising materials mentioning features or use of this software must
display the following acknowledgement: This product includes software
developed by Exotic Silicon.
5. The name of Exotic Silicon must not be used to endorse or promote products
derived from this software without specific prior written permission.
6. Redistributions of modified versions of the source code must be clearly
identified as having been modified from the original.
7. Redistributions in binary form that have been created from modified versions
of the source code must clearly state in the documentation and/or other
materials provided with the distribution that the source code has been
modified from the original.
THIS SOFTWARE IS PROVIDED 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
EXOTIC SILICON BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
/* This is the source code for es-srme version 1.0. */
/* For more details please visit: */
/* https://research.exoticsilicon.com/articles/resizing_softraid_volumes */
/* or gemini://gemini.exoticsilicon.com/articles/resizing_softraid_volumes */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <md5.h>
/* Magic number to identify softraid metadata */
#define MAGIC (uint64_t)0x4d4152436372616d
void usage(char * progname)
{
printf ("Usage: %s [input] [newsize]\n", progname);
printf ("Where [input] is a 512 byte softraid partition header, usually found in\nsector 16 of a RAID partition.\n");
printf ("If [newsize] is specified, new size values are written back to the header,\notherwise the current size values are simply displayed.\n");
return ;
}
int main(int argc, char *argv[], char *env[])
{
int fd;
unsigned char * buffer;
unsigned char * buffer_md5;
unsigned int raid_level;
unsigned int version;
unsigned int flag_bad_checksum;
uint64_t magic;
uint64_t size_vol;
uint64_t size_chunk;
uint64_t size_coerced;
uint64_t size_new;
MD5_CTX cont;
if (argc<2 || argc>3) {
usage(argv[0]);
return (3);
}
flag_bad_checksum=0;
buffer=malloc(512);
buffer_md5=malloc(MD5_DIGEST_LENGTH);
if (buffer == NULL || buffer_md5 == NULL) {
dprintf (STDERR_FILENO, "Error allocating buffers.\n");
return (3);
}
fd = open (argv[1], O_RDONLY);
if (fd == -1) {
dprintf (STDERR_FILENO, "Error opening %s for reading.\n", argv[1]);
return (3);
}
if (read (fd, buffer, 512) != 512) {
dprintf (STDERR_FILENO, "Error reading 512 bytes from %s.\n", argv[1]);
return (3);
}
close (fd);
/* Check magic */
magic=*(uint64_t *)buffer;
printf ("Read magic: %llx\n", magic);
if (magic != MAGIC) {
dprintf (STDERR_FILENO, "Bad magic, should be %llx.\n", MAGIC);
return (3);
}
/* We only support version 6 metadata */
version=*(long *)(buffer+8);
if (version != 0x06) {
dprintf (STDERR_FILENO, "Metadata version is %x, only version 6 is supported by this program.\n", version);
return (3);
}
/* We only support the crypto discipline */
raid_level=*(long *)(buffer+52);
if (raid_level != 0x43) {
dprintf (STDERR_FILENO, "This doesn't appear to be a softraid crypto volume.\n");
return (3);
}
size_vol=*(uint64_t *)(buffer+56);
size_chunk=*(uint64_t *)(buffer+208);
size_coerced=*(uint64_t *)(buffer+216);
printf ("Volume size is %llu sectors, %lluK, %lluM, %lluG\n",size_vol, size_vol/2, size_vol/2048, size_vol/2097152);
printf ("Chunk size is %llu sectors, %lluK, %lluM, %lluG\n",size_chunk, size_chunk/2, size_chunk/2048, size_chunk/2097152);
printf ("Coerced size is %llu sectors, %lluK, %lluM, %lluG\n",size_coerced, size_coerced/2, size_coerced/2048, size_coerced/2097152);
if (!((size_vol==size_chunk) && (size_chunk==size_coerced))) {
dprintf (STDERR_FILENO, "Mismatch between size fields, exiting...\n");
return (3);
}
/* Calculate the MD5 checksum of the first 96 bytes, which is the invariant volume metadata */
MD5Init (&cont);
MD5Update (&cont, buffer, 96);
MD5Final (buffer_md5, &cont);
printf ("Calculated volume MD5: %llx/%llx\n",*(uint64_t *)buffer_md5,*(uint64_t *)(buffer_md5+8));
printf (" Stored volume MD5: %llx/%llx\n",*(uint64_t *)(buffer+96),*(uint64_t *)(buffer+104));
/* Calculate the MD5 checksum of bytes 168 - 184 */
/* Certain buggy kernels calculate and store the wrong checksum in the chunk MD5 field */
/* We take the opportunity to detect this and optionally fix it */
MD5Init (&cont);
MD5Update (&cont, (buffer+168), 16);
MD5Final (buffer_md5, &cont);
if ( (*(uint64_t *)buffer_md5==*(uint64_t *)(buffer+240)) && (*(uint64_t *)(buffer_md5+8)==*(uint64_t *)(buffer+248)) ) {
printf ("Warning: This metadata seems to have been created with a buggy OpenBSD kernel!\n");
printf ("The chunk checksum matches bytes 168 - 184, instead of bytes 168 - 240.\n");
flag_bad_checksum=1;
}
/* Calculate the MD5 checksum of bytes 168 - 240, which is the invariant chunk metadata */
MD5Init (&cont);
MD5Update (&cont, (buffer+168), 72);
MD5Final (buffer_md5, &cont);
printf ("Calculated chunk MD5: %llx/%llx\n",*(uint64_t *)buffer_md5,*(uint64_t *)(buffer_md5+8));
printf (" Stored chunk MD5: %llx/%llx\n",*(uint64_t *)(buffer+240),*(uint64_t *)(buffer+248));
/* Exit here if we are in read-only mode, I.E. no new size was specified. */
if (argc==2) {
return (flag_bad_checksum);
}
/* Parse new size argument */
size_new=strtonum(argv[2],0,(uint64_t)1<<62,NULL);
if (size_new == 0) {
dprintf (STDERR_FILENO, "New size argument is invalid.\n");
return (3);
}
if (size_new == size_vol) {
printf ("Note: supplied size value is equal to existing value!\n");
}
/* Write new data to size fields */
printf ("New size is %llu sectors, %lluK, %lluM, %lluG\n",size_new, size_new/2, size_new/2048, size_new/2097152);
*(uint64_t *)(buffer+56)=size_new;
*(uint64_t *)(buffer+208)=size_new;
*(uint64_t *)(buffer+216)=size_new;
/* Re-calculate both checksums, and write them to the in-memory buffer */
/* Re-calculate the MD5 checksum of the first 96 bytes, which is the invariant volume metadata */
MD5Init (&cont);
MD5Update (&cont, buffer, 96);
MD5Final (buffer_md5, &cont);
printf (" New volume MD5: %llx/%llx\n",*(uint64_t *)buffer_md5,*(uint64_t *)(buffer_md5+8));
*(uint64_t *)(buffer+96)=*(uint64_t *)buffer_md5;
*(uint64_t *)(buffer+104)=*(uint64_t *)(buffer_md5+8);
/* Calculate the MD5 checksum of bytes 168 - 240, which is the invariant chunk metadata */
MD5Init (&cont);
MD5Update (&cont, (buffer+168), 72);
MD5Final (buffer_md5, &cont);
printf (" New chunk MD5: %llx/%llx\n",*(uint64_t *)buffer_md5,*(uint64_t *)(buffer_md5+8));
*(uint64_t *)(buffer+240)=*(uint64_t *)buffer_md5;
*(uint64_t *)(buffer+248)=*(uint64_t *)(buffer_md5+8);
/* Write the in-memory buffer over the original input file */
fd = open (argv[1], O_WRONLY | O_TRUNC);
if (fd == -1) {
dprintf (STDERR_FILENO, "Error opening %s for writing.\n", argv[1]);
return (3);
}
if (write (fd, buffer, 512) != 512) {
dprintf (STDERR_FILENO, "Error writing 512 bytes to %s.\n", argv[1]);
return (3);
}
close (fd);
return (2);
}