Commit be6a936c authored by Robert Sprowson's avatar Robert Sprowson
Browse files

Change to use objasm

Previously this component relied on BASIC to assemble the module (and patch in the default password etc).
Instead, use objasm so we can pull in all the normal system defines and make use of it's stricter checking.
A new tool "scrambler" performs the operation of patching in the default password.
Notes -
 This is a literal translation of former 'FSLockSRC', bugs and all.
 Put several unused files in the attic.
 Tested in an IOMD ROM.

Version 1.23. Tagged as 'FSLock-1_23'
parent 6d78e40c
hdr/** gitlab-language=armasm linguist-language=armasm linguist-detectable=true
s/** gitlab-language=armasm linguist-language=armasm linguist-detectable=true
*,ffb gitlab-language=bbcbasic linguist-language=bbcbasic linguist-detectable=true
c/** gitlab-language=c linguist-language=c linguist-detectable=true
......@@ -14,65 +14,30 @@
#
# Makefile for FSLock
#
# ***********************************
# *** C h a n g e L i s t ***
# ***********************************
# Date Name Description
# ---- ---- -----------
# 25-May-94 AMcC Created.
#
#
# Paths
#
EXP_HDR = <export$dir>
#
# Generic options:
#
MKDIR = cdir
AS = aasm
CP = copy
RM = remove
CCFLAGS = -c -depend !Depend -IC:
ASFLAGS = -depend !Depend -Stamp -quit -module -To $@ -From
CPFLAGS = ~cfr~v
#
# Program specific options:
#
COMPONENT = FSLock
SOURCE = FSLockSRC
TARGET = rm.FSLock # Warning! - target path is wired into the source
EXPORTS = ${EXP_HDR}.FSLock
#
# Generic rules:
#
rom: ${TARGET}
@echo AMU: warning - Hard coded SWI XOS_SynchroniseCodeAreas
@echo ${COMPONENT}: rom module built
HEADER1 = FSLock
CUSTOMROM = custom
export: ${EXPORTS}
@echo ${COMPONENT}: export complete
include StdTools
include AAsmModule
install_rom: ${TARGET}
${CP} ${TARGET} ${INSTDIR}.${COMPONENT} ${CPFLAGS}
@echo ${COMPONENT}: rom module installed
rom_common: ${ROM_MODULE} scrambler
${RUN}scrambler ${ROM_MODULE}
clean:
${RM} ${TARGET}
@echo ${COMPONENT}: cleaned
rom: rom_common
@${ECHO} ${COMPONENT}: rom module built & scrambled
resources:
${MKDIR} ${RESDIR}.${COMPONENT}
${CP} Resources.${LOCALE}.Messages ${RESDIR}.${COMPONENT}.Messages ${CPFLAGS}
@echo ${COMPONENT}: resource files copied
install_rom: rom_common
${CP} ${ROM_MODULE} ${INSTDIR}.${TARGET} ${CPFLAGS}
${CP} ${ROM_MODULE}_gpa ${INSTDIR}.${TARGET}_gpa ${CPFLAGS}
@${ECHO} ${COMPONENT}: rom module installed
${TARGET}: ${SOURCE}
Run FSLockSrc
clean::
${XWIPE} od ${WFLAGS}
${RM} scrambler
${EXP_HDR}.${COMPONENT}: hdr.${COMPONENT}
${CP} hdr.${COMPONENT} $@ ${CPFLAGS}
scrambler: scrambler.c
${MAKE} -f scrambler/mk THROWBACK=${THROWBACK}
# Dynamic dependencies:
......@@ -14,3 +14,4 @@
|
Dir <Obey$Dir>
amu_machine clean
stripdepnd
\ No newline at end of file
......@@ -13,4 +13,4 @@
| limitations under the License.
|
Dir <Obey$Dir>
amu_machine rom
amu_machine rom THROWBACK=-throwback
;
; This file is automatically maintained by srccommit, do not edit manually.
; Last processed by srccommit version: 1.1.
;
GBLS Module_MajorVersion
GBLA Module_Version
GBLS Module_MinorVersion
GBLS Module_Date
GBLS Module_FullVersion
GBLS Module_ApplicationDate
GBLS Module_HelpVersion
GBLS Module_ComponentName
GBLS Module_ComponentPath
Module_MajorVersion SETS "1.23"
Module_Version SETA 123
Module_MinorVersion SETS ""
Module_Date SETS "25 Sep 2014"
Module_ApplicationDate SETS "25-Sep-14"
Module_ComponentName SETS "FSLock"
Module_ComponentPath SETS "castle/RiscOS/Sources/FileSys/FSLock"
Module_FullVersion SETS "1.23"
Module_HelpVersion SETS "1.23 (25 Sep 2014)"
END
/* (1.22)
/* (1.23)
*
* This file is automatically maintained by srccommit, do not edit manually.
* Last processed by srccommit version: 1.1.
*
*/
#define Module_MajorVersion_CMHG 1.22
#define Module_MajorVersion_CMHG 1.23
#define Module_MinorVersion_CMHG
#define Module_Date_CMHG 11 Jun 2009
#define Module_Date_CMHG 25 Sep 2014
#define Module_MajorVersion "1.22"
#define Module_Version 122
#define Module_MajorVersion "1.23"
#define Module_Version 123
#define Module_MinorVersion ""
#define Module_Date "11 Jun 2009"
#define Module_Date "25 Sep 2014"
#define Module_ApplicationDate "11-Jun-09"
#define Module_ApplicationDate "25-Sep-14"
#define Module_ComponentName "FSLock"
#define Module_ComponentPath "castle/RiscOS/Sources/FileSys/FSLock"
#define Module_FullVersion "1.22"
#define Module_HelpVersion "1.22 (11 Jun 2009)"
#define Module_LibraryVersionInfo "1:22"
#define Module_FullVersion "1.23"
#define Module_HelpVersion "1.23 (25 Sep 2014)"
#define Module_LibraryVersionInfo "1:23"
/* Copyright 2014 Castle Technology Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define DEFAULT_PASSWORD "TheDefaultPassword"
#define TITLESTRING "FSLock"
#define CRC_POLY 0xA001 /* Per OS_CRC */
static uint32_t crypt_passwd(const char *string)
{
uint32_t sum = 0;
uint8_t rot;
while (*string >= ' ')
{
sum = (sum << 1) + *string;
if (*string & 1)
{
rot = *string & 0x1F;
sum = (sum >> rot) | (sum << (32 - rot));
}
string++;
}
return sum;
}
static void stringcopy(char *dst, const char *src, size_t length)
{
int8_t eormax = strlen(TITLESTRING) - 1;
int8_t eoridx = -1;
size_t i;
for (i = 0; i < length; i++)
{
if (eoridx < 0) eoridx = eormax;
dst[i] = src[i] ^ TITLESTRING[eoridx];
eoridx--;
}
/* Always terminate the string */
dst[length] = 0;
}
static uint32_t os_crc(uint32_t crc, void *start, void *end, int32_t stride)
{
uint8_t *src = (uint8_t *)start;
size_t bits;
bool carry;
while (src != end)
{
crc = crc ^ ((*src) << 0);
src = src + stride;
bits = 8;
if (src != end)
{
/* Even stride, consume another byte */
crc = crc ^ ((*src) << 8);
src = src + stride;
bits = 16;
}
while (bits)
{
carry = (crc & 1) != 0;
crc = crc >> 1;
if (carry) crc = crc ^ CRC_POLY;
bits--;
}
}
return crc;
}
int main(int argc, char *argv[])
{
FILE *f;
uint32_t modulesize, pwd;
char *module;
struct
{
uint32_t sig_lo;
#define SIG_LO 0xE7FF1234uL
uint32_t crc_value;
uint32_t default_password;
uint32_t crypt_passwd;
uint32_t rnd1;
uint32_t rnd2;
#define PATCH_OFFSET(k) ((k) & 0xFFFFFF)
#define PATCH_SIZE(k) ((k) >> 24)
uint32_t sig_hi;
#define SIG_HI 0xE7FF5678uL
} patches;
if (argc != 2)
{
printf("%s: Bad args\n", argv[0]);
exit(EXIT_FAILURE);
}
/* Get everything we need to know */
f = fopen(argv[1], "rb");
fseek(f, 0, SEEK_END);
modulesize = (uint32_t)ftell(f);
fseek(f, -sizeof(patches), SEEK_END);
if (fread(&patches, sizeof(patches), 1, f) != 1)
{
printf("%s: Can't read patch table\n", argv[0]);
exit(EXIT_FAILURE);
}
if ((patches.sig_lo != SIG_LO) || (patches.sig_hi != SIG_HI))
{
/* Already patched this one */
fclose(f);
return EXIT_SUCCESS;
}
/* Read in the unscrambled binary */
fseek(f, 0, SEEK_SET);
module = malloc(modulesize);
if (module == NULL)
{
printf("%s: Out of memory\n", argv[0]);
exit(EXIT_FAILURE);
}
if (fread(module, sizeof(*module), modulesize, f) != modulesize)
{
printf("%s: Can't load module\n", argv[0]);
exit(EXIT_FAILURE);
}
fclose(f);
/* State what we know */
printf("Scrambling '%s'\n", argv[1]);
printf(" - Default password at +%X.\n", PATCH_OFFSET(patches.default_password));
printf(" - Crypt code at +%X length %u.\n", PATCH_OFFSET(patches.crypt_passwd),
PATCH_SIZE(patches.crypt_passwd));
printf(" - Rand 1 at +%X.\n", PATCH_OFFSET(patches.rnd1));
printf(" - Rand 2 at +%X.\n", PATCH_OFFSET(patches.rnd2));
printf(" - CRC at +%X.\n", PATCH_OFFSET(patches.crc_value));
/* Set a default password */
pwd = crypt_passwd(DEFAULT_PASSWORD);
*(uint32_t *)&module[PATCH_OFFSET(patches.default_password)] = pwd;
/* Sprinkle in some entropy */
srand(time(NULL));
*(uint32_t *)&module[PATCH_OFFSET(patches.rnd1)] = rand() ^ (rand() << 8);
*(uint32_t *)&module[PATCH_OFFSET(patches.rnd2)] = rand() ^ (rand() << 8);
/* Cloak the decrypter */
stringcopy(&module[PATCH_OFFSET(patches.crypt_passwd)],
&module[PATCH_OFFSET(patches.crypt_passwd)],
PATCH_SIZE(patches.crypt_passwd));
/* CRC the result up to but not including the CRC */
*(uint32_t *)&module[PATCH_OFFSET(patches.crc_value)] = os_crc(pwd,
module,
&module[PATCH_OFFSET(patches.crc_value)],
1);
/* Write it back out */
f = fopen(argv[1], "wb");
if (fwrite(module, modulesize - sizeof(patches), 1, f) != 1)
{
printf("%s: Failed writing module\n", argv[0]);
exit(EXIT_FAILURE);
}
free(module);
fclose(f);
return EXIT_SUCCESS;
}
This diff is collapsed.
# Copyright 2014 Castle Technology Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Makefile for scrambler
#
COMPONENT = scrambler
include HostTools
include AppLibs
include CApp
# Dynamic dependencies:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment