extlinux: add --device option to override device detect
[syslinux/sherbszt.git] / core / fs / loadhigh.c
blobbd9d35354d4115d6e160ac4435616e260d49c0b2
1 /*
2 * -----------------------------------------------------------------------
4 * Copyright 1994-2009 H. Peter Anvin - All Rights Reserved
5 * Copyright 2009-2010 Intel Corporation; author: H. Peter Anvin
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
10 * Boston MA 02111-1307, USA; either version 2 of the License, or
11 * (at your option) any later version; incorporated herein by reference.
13 * ----------------------------------------------------------------------- */
16 * loadhigh.c
18 * An alternate interface to getfssec.
20 * Inputs: SI = file handle/cluster pointer
21 * EDI = target address in high memory
22 * EAX = maximum number of bytes to load
23 * DX = zero-padding mask (e.g. 0003h for pad to dword)
24 * BX = 16-bit subroutine to call at the top of each loop
25 * (to print status and check for abort)
26 * EBP = maximum load address
28 * Outputs: SI = file handle/cluster pointer
29 * EBX = first untouched address (not including padding)
30 * EDI = first untouched address (including padding)
31 * CF = reached EOF
32 * OF = ran out of high memory
35 #include <com32.h>
36 #include <minmax.h>
37 #include "core.h"
38 #include "fs.h"
40 #define MAX_CHUNK (1 << 20) /* 1 MB */
42 void pm_load_high(com32sys_t *regs)
44 struct fs_info *fs;
45 uint32_t bytes;
46 uint32_t zero_mask;
47 bool have_more;
48 uint32_t bytes_read;
49 char *buf, *limit;
50 struct file *file;
51 uint32_t sector_mask;
52 size_t pad;
53 uint32_t retflags = 0;
55 bytes = regs->eax.l;
56 zero_mask = regs->edx.w[0];
57 buf = (char *)regs->edi.l;
58 limit = (char *)(regs->ebp.l & ~zero_mask);
59 file = handle_to_file(regs->esi.w[0]);
60 fs = file->fs;
62 sector_mask = SECTOR_SIZE(fs) - 1;
64 while (bytes) {
65 uint32_t sectors;
66 uint32_t chunk;
68 if (buf + SECTOR_SIZE(fs) > limit) {
69 /* Can't fit even one more sector in... */
70 retflags = EFLAGS_OF;
71 break;
74 chunk = bytes;
76 if (regs->ebx.w[0]) {
77 call16((void (*)(void))(size_t)regs->ebx.w[0], &zero_regs, NULL);
78 chunk = min(chunk, MAX_CHUNK);
81 if (chunk > (((char *)limit - buf) & ~sector_mask))
82 chunk = ((char *)limit - buf) & ~sector_mask;
84 sectors = (chunk + sector_mask) >> SECTOR_SHIFT(fs);
85 bytes_read = fs->fs_ops->getfssec(file, buf, sectors, &have_more);
87 if (bytes_read > chunk)
88 bytes_read = chunk;
90 buf += bytes_read;
91 bytes -= bytes_read;
93 if (!have_more) {
95 * If we reach EOF, the filesystem driver will have already closed
96 * the underlying file... this really should be cleaner.
98 _close_file(file);
99 regs->esi.w[0] = 0;
100 retflags = EFLAGS_CF;
101 break;
105 pad = (size_t)buf & zero_mask;
106 if (pad)
107 memset(buf, 0, pad);
109 regs->ebx.l = (size_t)buf;
110 regs->edi.l = (size_t)buf + pad;
111 set_flags(regs, retflags);