Adding upstream version 4.01+dfsg.
[syslinux-debian/hramrach.git] / core / fs / loadhigh.c
blobe365b1a36ad77f6fc80cde21b4c0b13a45d028f0
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;
54 bytes = regs->eax.l;
55 zero_mask = regs->edx.w[0];
56 buf = (char *)regs->edi.l;
57 limit = (char *)(regs->ebp.l & ~zero_mask);
58 file = handle_to_file(regs->esi.w[0]);
59 fs = file->fs;
61 regs->eflags.l &= ~(EFLAGS_CF|EFLAGS_OF|EFLAGS_AF|
62 EFLAGS_PF|EFLAGS_ZF|EFLAGS_SF);
64 sector_mask = SECTOR_SIZE(fs) - 1;
66 while (bytes) {
67 uint32_t sectors;
68 uint32_t chunk;
70 if (buf + SECTOR_SIZE(fs) > limit) {
71 /* Can't fit even one more sector in... */
72 regs->eflags.l |= EFLAGS_OF;
73 break;
76 chunk = bytes;
78 if (regs->ebx.w[0]) {
79 call16((void (*)(void))(size_t)regs->ebx.w[0], &zero_regs, NULL);
80 chunk = min(chunk, MAX_CHUNK);
83 if (chunk > (((char *)limit - buf) & ~sector_mask))
84 chunk = ((char *)limit - buf) & ~sector_mask;
86 sectors = (chunk + sector_mask) >> SECTOR_SHIFT(fs);
87 bytes_read = fs->fs_ops->getfssec(file, buf, sectors, &have_more);
89 if (bytes_read > chunk)
90 bytes_read = chunk;
92 buf += bytes_read;
93 bytes -= bytes_read;
95 if (!have_more) {
97 * If we reach EOF, the filesystem driver will have already closed
98 * the underlying file... this really should be cleaner.
100 _close_file(file);
101 regs->esi.w[0] = 0;
102 regs->eflags.l |= EFLAGS_CF;
103 break;
107 pad = (size_t)buf & zero_mask;
108 if (pad)
109 memset(buf, 0, pad);
111 regs->ebx.l = (size_t)buf;
112 regs->edi.l = (size_t)buf + pad;